2013-08-19 127 views
2

我使用下面的代碼來顯示同一個標籤的文章列表,裏面的single.php:如何顯示「沒有發現帖子」的wordpress標籤相關帖子?

<?php 
if (is_single()) { 
    $tags = wp_get_post_tags($post->ID); 
    if ($tags) { 
    $first_tag = $tags[0]->term_id; 
echo 'first_tag' .$first_tag; 
$args=array(
    'tag__in' => array($first_tag), 
    'post__not_in' => array($post->ID), 
    'showposts'=>5 
); 
$my_query = new WP_Query($args); 
if($my_query->have_posts()) { 
    echo 'Related Posts'; 
    while ($my_query->have_posts()) : $my_query->the_post(); ?> 
    <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php  the_title_attribute(); ?>"><?php the_title(); ?></a></p> 
    <?php 
    endwhile; 
} //if ($my_query) 
    } //if ($tags) 
    wp_reset_query(); // Restore global post data stomped by the_post(). 
} //if (is_single()) 
?> 

不過,我想它顯示類似的,當「沒有發現帖子」有沒有其他標籤相同的帖子。

任何想法,我可能會做到這一點?

回答

0

只需添加一個else語句,如果($ myquery-> havePosts)

... 
    <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php  the_title_attribute(); ?>"><?php the_title(); ?></a></p> 
    <?php 
    endwhile; 
} //if ($my_query) 
else{ 
    echo "No Posts" //Look I am HERE 
} 
    } //if ($tags) 
+0

好極了,工作就像一種享受!非常感謝你。 –

相關問題