wordpress中强大的调用文章函数query posts 用法

2019-02-19 17:13:37王振洲

6、Time Parameters(时间参数)

显示12月20日发表的文章列表。


<?php
query_posts(‘monthnum=12&day=20′);
?>

显示本周发表文章列表。


<?php
$week = date(‘W’);
$year = date(‘Y’);
query_posts(‘year=’ . $year .‘&w=’ .$week );
?>

显示最近30天内发表的文章列表。


<pre name="code" class="php"><?php
function filter_where($where = ”) {
$where .= ” AND post_date > ‘” . date(‘Ym-d’, strtotime(‘-30 days’)) . “‘”;
return $where;
}
add_filter(‘posts_where’, ‘filter_where’);
query_posts($query_string);
?></pre>


<pre></pre>


<span style="color:#507090"></span>

7、Orderby Parameters(排列顺序参数)
<?php
//依照发表作者排列
orderby=author
//依照日期排列
orderby=date
//依照标题排列
orderby=title
//依照最后编辑时间排列
orderby=modified
//依照分页顺序排列(仅适用于分页)
orderby=menu_order
// (不知道XD…)
orderby=parent
//依照文章编号排列
orderby=ID
//随机排列
orderby=rand
//依照自订栏位数值排列
orderby=meta_value
//依照预设排列
orderby=none
//依照回响数排列
orderby=comment_count
?>

8、Pagination Parameters(分页参数)


<?php
//当值设定true时则为不分页显示,直接显示全部文章
nopaging=true
//显示每页文章显示10篇
posts_per_page=10
//页数,例如当设定为6时则就表示跳到第6页
paged=6
//排列顺序,ASC为按时间顺序排列文章,若是DESC则是反向显示文章
order=ASC
?>

9、组合运用范例

显示分类编号为3且是在2004年发表的文章。


<?php
query_posts(‘cat=3&year=2004′);
?>
显示分类编号为1及3且每页显示两篇、依照标题逆向排列的文章。
<?php
query_posts(array(‘category__and’=>array(1,3),‘posts_per_page’=>2,‘orderby’=>title,‘order’=>DESC));
?>
仅在首页显示,并且是在分类编号为13的当月发表文章。
<?php
if (is_home()) {
query_posts($query_string . ‘&cat=13&monthnum=’ . date(‘n’,current_time(‘timestamp’)));
}
?>
显示分类编号为1且标签为apples的文章。
<?php
query_posts(‘cat=1&tag=apples’);
?>