wordpress根据标签(tag)获取文章列表


主题开发过程中,经常需要根据指定的标签(tag)来获取文章列表,query_posts函数貌似无法实现,我们可以使用WP_Query来实现该功能,代码如下:

  1. <?php
  2. $tag = ''; //标签名
  3. $args=array(
  4. 'tag' => $tag',
  5. 'showposts'=>5, //输出的文章数量
  6. 'caller_get_posts'=>1
  7. );
  8. $my_query = new WP_Query($args);
  9. if( $my_query->have_posts() ) {
  10. echo '5篇指定标签文章输出:';
  11. while ($my_query->have_posts()) : $my_query->the_post(); ?>
  12. <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
  13. <?php
  14. endwhile;
  15. }
  16. wp_reset_query();
  17. ?>