WordPress调用同分类随机文章

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

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

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

 

 

 

  • QQ群:253510359
  • 建议:VPS商家层出不穷,根据需要购买,切莫剁剁剁!
  • 评测:很多VPS虽已评测,但网络环境改变稳定性,速度也会随之改变.评测只能作为一般性参考.不负任何法律,道义责任.
  • 申明:所有vps,域名,服务器优惠信息均来自网络公开内容,由于水平有限不免有谬误.请以官方为准.