首页 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="<?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="<?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(); ?>

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

 

-=||=-收藏赞 (0)
更多主题
v1.0.0
小程序收录免费WordPress主题:XCX主题
¥ 0 关注:22,483
详情
单栏极简文艺WordPress博客主题:Diaspora主题
¥ 0 关注:22,055
详情
简约黑白WordPress个人博客主题:Personal主题
¥ 0 关注:21,760
详情
功能丰富切轻量的WordPress虚拟资源主题:Rizhuti主题
¥ 599 关注:19,813
详情
v1.0.0
一款简约单栏的免费WordPress博客主题:itheme主题
¥ 0 关注:19,737
详情
v3.4.0
适用自媒体资讯的免费WordPress主题:Autumn主题
¥ 0 关注:16,836
详情
回复
暂无评论

不要再留垃圾评论了,主题君整理资源不容易,留几句鼓励的话吧。