我想實現下面的代碼的變體。此代碼目前顯示帖子的附件標題。PHP - 顯示附件標題,如果爲空,顯示帖子標題
我該如何修改代碼,以便在沒有附件的情況下顯示帖子標題。
這是拉第一個附件標題的代碼。
<?php $args = array('post_type' => 'attachment', 'orderby' => 'menu_order', 'order' => 'ASC', 'post_mime_type' => 'image' ,'post_status' => null, 'numberposts' => 1, 'post_parent' => $post->ID);
$attachments = get_posts($args);
if ($attachments) {
foreach ($attachments as $attachment) {
$image_title = $attachment->post_title;?>
<?php echo $image_title; ?><?php } } ?>
謝謝你的最佳途徑。這是我想要的。 – Recofa
沒問題,樂於幫忙。 – mattcouchman
@Recofa - 如果這是正確的答案,請標記爲正確。 –