怎么使用query调用WordPress同分类随机文章

2024年1 月26日 / 网站源码 / 没有评论 / 1,362次

query调用真的很方便也很强大。居于修改主题需要,所以尝试了传统方法《为博客添加同分类随机文章》,但是由于和原主题文章统计有冲突,所以网上查找了下,感觉不错,于是收藏之。

  1. <ul>
  2. <?php
  3.  $cat = get_the_category();
  4. foreach($cat as $key=>$category){
  5.    $catid = $category->term_id;    }
  6. $args = array('orderby' => 'rand','showposts' => 8,'cat' => $catid );
  7. $query_posts = new WP_Query();    $query_posts->query($args);
  8.  while ($query_posts->have_posts()) : $query_posts->the_post();    ?>
  9.  <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
  10. <?php endwhile;?>
  11. <?php wp_reset_query(); ?></ul>

至于样式和其他简单的html修改,自己根据需要进行调整。而且可以在arra里添加根据自己需求的条件输出。