WordPress根据标签获取文章列表

来自:互联网
时间:2019-09-12
阅读:

在对 WordPress 进行二次开发时,有时候需要根据已经添加的标签来获取相应的文章列表。这时候可以使用 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(); ?>
返回顶部
顶部