当前位置:首页WP教程WordPress如何调用全站随机文章/同分类随机文章

WordPress如何调用全站随机文章/同分类随机文章

看过上一篇WordPress教程的朋友应该已经学会如何调用最新文章、热门文章以及指定分类文章了,但是我们也许会用到更多的调用文章方式,例如:调用随机文章,那么我…

看过上一篇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(); ?>

是不是很简单呢?只需要在需要调用随机文章的页面,加入上面任意一种代码即可!

给TA打赏
共{{data.count}}人
人已打赏
WP教程

WordPress如何调用最新/热门/指定分类文章

2023-3-8 22:16:45

WP教程

WordPress如何去掉分类链接中的category

2023-3-8 22:16:48

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
购物车
优惠劵
有新私信 私信列表
搜索