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

2022年11月28日 发表评论

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

怎么调用WordPress同分类随机文章?在适当的位置显示调用随机文章可以促进网站内链,增加文章阅读点击量,有利于SEO,网上大部分Wordpress调用随机文章代码都是基于全站文章,这里发一个调用同分类随机文章的代码。

1、将下面代码放到主题文章页面single模板或者边栏sidebar模板适当位置即可:

  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>  

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: