首页 WordPress教程 WordPress查询文章函数:query_posts
正文 评论

WordPress查询文章函数:query_posts

query_posts函数是WordPress查询文章里面比较常用的函数,当然WordPress调用文章函数有很多,今天主题君主要给大家讲解下query_posts这个函数。

query_posts的基本用法

<?php query_posts( $args ); ?>
<?php while ( have_posts() ) : the_post(); ?>
...
<?php endwhile; ?>
<?php wp_reset_query(); ?>

query_posts函数添加在主循环前,如果后面还有循环函数,需要在循环结束后增加<?php wp_reset_query(); ?>来重置query。

使用query_posts获取多属性文章

cat = 1 就是获取分类为1的文章、posts_per_page是文章数量

<?php 
	$args=array(
	    'cat' => '1',
	    'posts_per_page'=> '5',
	    'orderby'=> 'date'
	);
?>
<?php while ( have_posts() ) : the_post(); ?>
... 
<?php endwhile; ?>
<?php wp_reset_query(); ?>
  • orderby=date?按发布日期排序
  • orderby=modified 按修改时间排序
  • orderby=ID 按文章ID排序
  • orderby=comment_count 按评论最多排序
  • orderby=title 按标题排序
  • orderby=rand 随机排序
-=||=-收藏赞 (0)
更多主题
v1.0.0
小程序收录免费WordPress主题:XCX主题
¥ 0 关注:22,635
详情
单栏极简文艺WordPress博客主题:Diaspora主题
¥ 0 关注:22,135
详情
简约黑白WordPress个人博客主题:Personal主题
¥ 0 关注:21,864
详情
功能丰富切轻量的WordPress虚拟资源主题:Rizhuti主题
¥ 599 关注:19,929
详情
v1.0.0
一款简约单栏的免费WordPress博客主题:itheme主题
¥ 0 关注:19,841
详情
WordPress版WebStack导航主题:WebStack主题
¥ 0 关注:16,925
详情
回复
暂无评论

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