2013-08-19 204 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

好极了,工作就像一种享受!非常感谢你。 –

相关问题