看过上一篇WordPress教程的朋友应该已经学会如何调用最新文章、热门文章以及指定分类文章了,但是我们也许会用到更多的调用文章方式,例如:调用随机文章,那么我们改如何调用到全部文章进行随机显示,或者是调用同一个分类下的随机文章呢?
下面主题君带来一段WordPress教程,看完你就明白了!
整站随机文章
<?php
$args = array(
'numberposts' => 5, //文章数量 5
'orderby' => 'rand', //排序 随机
'post_status' => 'publish' //发布状态 已发布
);
$rand_posts = get_posts( $args );
foreach( $rand_posts as $post ) : ?>
<a href="https://www.ztjun.com/<?php the_permalink(); ?>" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><?php the_title(); ?></a>
<?php endforeach; ?>
同分类随机文章
<?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();
?>
<a href="https://www.ztjun.com/<?php the_permalink(); ?>" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><?php the_title(); ?></a>
<?php endwhile; wp_reset_query(); ?>
是不是很简单呢?只需要在需要调用随机文章的页面,加入上面任意一种代码即可!
文章标题:WordPress如何调用全站随机文章/同分类随机文章
文章链接:https://www.jitheme.com/2695.html
更新时间:2024年03月28日
1.本站大部分内容均收集于网络!若内容若侵犯到您的权益,请发送邮件至:service@jitheme.com,我们将第一时间处理!
2.资源所需价格并非资源售卖价格,是收集、整理、编辑详情以及本站运营的适当补贴,并且本站不提供任何免费技术支持。
3.所有资源仅限于参考和学习,版权归原作者所有,更多请阅读极(子)主题网络服务协议。