主题开发过程中,经常需要根据指定的标签(tag)来获取文章列表,query_posts函数貌似无法实现,我们可以使用WP_Query来实现该功能,代码如下:
- <?php
- $tag = ''; //标签名
- $args=array(
- 'tag' => $tag',
- 'showposts'=>5, //输出的文章数量
- 'caller_get_posts'=>1
- );
- $my_query = new WP_Query($args);
- if( $my_query->have_posts() ) {
- echo '5篇指定标签文章输出:';
- 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;
- }
- wp_reset_query();
- ?>