zoukankan      html  css  js  c++  java
  • (转)WordPress的主查询函数-query_posts()

    今天说说WordPress 的主查询函数 -query_posts(),因为我正在制作的主题里面多次用到了这个函数 。

    query_posts()查询函数决定了哪些文章出现在WordPress 主 循环(loop)中,正因为如此,query_posts函数仅用于修改主页循环(Loop),而不是在页面上生成次级循环。如果你希望在主循环外另外生 成循环,应该新建独立的WP_Query对象,用这些对象生成循环。在主循环外的循环上使用query_posts会导致主循环运行偏差,并可能在页面上 显示出你不希望看到的内容。

    query_posts()查询函数函数接收大量参数,格式与URL中的参数格式相同(如p=4表示ID为4的文章)。下面就举例说说query_posts函数的一些常用的语法格式。

    1.从博客主页上排除某些分类目录

    将以下代码添加到index.php文件中,使主页显示的文章可以来自除分类3以外的任何分类。

    Php代码  收藏代码
    1. <?php  
    2.   if (is_home()) {  
    3.     query_posts("cat=-3");  
    4.   }  
    5. ?>  

    你也可以多排除几个分类。

    Php代码  收藏代码
    1. <?php  
    2.   if (is_home()) {  
    3.     query_posts("cat=-1,-2,-3");  
    4.   }  
    5. ?>  
    2.查询指定文章

    用以下语句检索某篇指定文章:

    Php代码  收藏代码
    1. <?php  
    2. //获取ID值为5的文章  
    3. query_posts('p=5');  
    4. ?>  

    如果你希望在查询语句中使用Read More功能,请将全局变量$more设为0。

    Php代码  收藏代码
    1. <?php  
    2. //获取ID值为5的页面  
    3. query_posts('p=5');  
    4.   
    5. global $more;  
    6. //初始化$more  
    7. $more = 0;  
    8.   
    9. //循环查询到的结果  
    10. while (have_posts()) : the_post();  
    11. the_content('Read the full post ?');  
    12. endwhile;  
    13. ?>  
    3.检索指定页面

    用以下语句检索某篇指定页面:

    Php代码  收藏代码
    1. <?php  
    2. query_posts('page_id=7'); //获取页面ID为7的页面  
    3. ?>  

    或者

    Php代码  收藏代码
    1. <?php  
    2. query_posts('pagename=about');  
    3. ?>  

    检索子页面时,需要提供子页面及其父页面的别名,用斜线隔开两者。例如:

    Php代码  收藏代码
    1. <?php  
    2. query_posts('pagename=parent/child');  
    3. ?>  

    上面都是采取 query_posts($query_string) 的形式来调用该函数,下面介绍另一种方法,用数组传递参数变量。

    Php代码  收藏代码
    1. query_posts(array(  
    2.   'cat' => 22,  
    3.   'year' => $current_year,  
    4.   'monthnum' => $current_month,  
    5.   'order' => 'ASC',  
    6. ));  

    相比字符串方式,数组形式更加形象直观,不容易出错。

    下面整理一些经常要用到的参数,有些是我用过的,有些则没有,算作归纳吧。

    分类参数

    只显示特定分类下的文章。

    • cat —— 必须使用分类ID
    • category_name
    • category_and —— 必须使用分类ID
    • category_in —— 必须使用分类ID
    • category_not_in —— 必须使用分类ID

    根据ID显示单个分类

    只显示来自某一个分类目录ID(以及该分类目录下的子分类目录)的文章:

    Php代码  收藏代码
    1. query_posts('cat=4');  

    根据分类名称显示单个分类

    只显示来自某一个分类名称下的文章:

    Php代码  收藏代码
    1. query_posts('category_name=Staff Home');  

    根据ID显示多个分类

    显示来自若干指定分类目录ID下的文章:

    Php代码  收藏代码
    1. query_posts('cat=2,6,17,38');  

    排除某一分类中的文章

    显示除某一分类文章外的所有文章,被排除的分类ID以减号(’-')作为前缀。

    Php代码  收藏代码
    1. query_posts('cat=-3');  

    以上代码删除ID为3的分类中的文章。

    处理多个分类

    显示隶属于多个分类的文章。下面的代码可展示同时属于分类2和分类6的文章:

    Php代码  收藏代码
    1. query_posts(array('category__and' => array(2,6)));  

    如果希望显示分类2或分类6中的文章,可以使用上面介绍的cat,也可以使用category_in函数 (注意这里不会显示分类下子分类中的文章) :

    Php代码  收藏代码
    1. query_posts(array('category__in' => array(2,6)));  

    可以用下面这种方式排除多个分类中的文章:

    Php代码  收藏代码
    1. query_posts(array('category__not_in' => array(2,6)));  

    标签参数

    显示特定标签下的文章。

    • tag —— 必须使用标签ID
    • tag_id —— 必须使用标签ID
    • tag_and —— 必须使用标签ID
    • tag_in —— 必须使用标签ID
    • tag_not_in —— 必须使用标签ID
    • tag_slug_and ——必须使用标签ID
    • tag_slug_in ——必须使用标签ID

    获取某一标签中的文章

    Php代码  收藏代码
    1. query_posts('tag=cooking');  

    获取若干标签中任一标签中的文章

    Php代码  收藏代码
    1. query_posts('tag=bread+baking+recipe');  

    多个标签

    显示同时属于ID为37和47的标签下的文章:

    Php代码  收藏代码
    1. query_posts(array('tag__and' => array(37,47));  

    若要显示ID为为37或47的标签下的文章,可以使用tag参数,也可以用tag_in:

    Php代码  收藏代码
    1. query_posts(array('tag__in' => array(37,47));  

    显示的文章既不属于标签37,也不属于标签47:

    Php代码  收藏代码
    1. query_posts(array('tag__not_in' => array(37,47));  

    tag_slug_in与tag_slug_and工作方式几乎一致,不同之处在于相匹配的别名不同。

     

    作者参数

    你也可以根据作者来选择文章。

    • author=3
    • author=-3 ——排除ID为3的作者所发表的文章
    • author_name=Harriet

    注意:author_name运行在user_nicename字段上,同时author运行在author id字段上。

    显示ID为1的作者所发表的所有页面,以标题顺序排列页面,页面列表上方无置顶文章:

    Php代码  收藏代码
    1. query_posts('caller_get_posts=1&author=1&post_type=page&post_status=publish&orderby=title&order=ASC');  

    文章&页面参数

    检索单篇文章或页面。

    • ‘p’ => 27 —— 通过文章ID显示该文章
    • ‘name’ => ‘about-my-life’ —— 对某篇文章的查询,查询中含有文章别名
    • ‘page_id’ => 7 —— 对ID为7的页面的查询
    • ‘pagename’ => ‘about’ —— 注意,这不是页面标题,而是页面路径
    • 用’posts_per_page’ => 1 – use ‘posts_per_page’ => 3 展示3篇文章。用’posts_per_page’ => -1展示所有文章
    • ‘showposts’ => 1 – use ‘showposts’ => 3 展示3篇文章。用’showposts’ => -1展示所有文章。已弃用。
    • ‘post__in’ => array(5,12,2,14,7) —— 指定希望检索的文章ID
    • ‘post__not_in’ => array(6,2,8) ——排除不希望检索的文章ID
    • ‘post_type’ => ‘page’ ——返回页面;默认值为post;可用值包括any, attachment, page, post或revision。any可检索到除修订版外的所有页面类型。
    • ‘post_status’ => ‘publish’ —— 返回已发布页面。可用值还包括pending, draft, future, private, trash。关于inherit请见get_children。trash状态新增于WordPress  2.9。
    • ‘post_parent’ => 93 —— 返回页面93的子页面。

     

    置顶文章参数

    置顶文章功能引入于WordPress 2.7。在查询中,被设为“置顶”的文章会显示在其它文章之前,除非该文章已经被caller_get_posts=1 参数排除。

    • array(‘post__in’=>get_option(‘sticky_posts’)) —— 返回所有置顶文章的数组
    • caller_get_posts=1 —— 排除返回的文章上方的置顶文章,但在返回文章列表时,以自然顺序将曾经置顶的文章安插在列表中。

    返回第一篇置顶文章

    Php代码  收藏代码
    1. $sticky=get_option('sticky_posts') ;  
    2. query_posts('p=' . $sticky[0]);  

    Php代码  收藏代码
    1. $args = array(  
    2. 'posts_per_page' => 1,  
    3. 'post__in' => get_option('sticky_posts'),  
    4. 'caller_get_posts' => 1  
    5. );  
    6. query_posts($args);  

    注意:第二种方法只能返回最新发表的置顶文章;若当前无置顶文章,返回最新发表文章。

    返回第一篇置顶文章;若无,则不返回任何内容

    Php代码  收藏代码
    1. $sticky = get_option('sticky_posts');  
    2. $args = array(  
    3. 'posts_per_page' => 1,  
    4. 'post__in' => $sticky,  
    5. 'caller_get_posts' => 1  
    6. );  
    7. query_posts($args);  
    8. if($sticky[0]) {  
    9. // insert here your stuff...  
    10. }  

    从查询中排除所有置顶文章

    Php代码  收藏代码
    1. query_posts(array("post__not_in" =>get_option("sticky_posts")));  

    返回某一分类下所有文章,但不在文章列表上方显示置顶文章。所有设为“置顶”的文章以正常顺序(如日期顺序)显示

    Php代码  收藏代码
    1. query_posts('caller_get_posts=1&posts_per_page=3&cat=6');  

    返回某一分类下所有文章,完全不显示置顶文章,保留分页

    Php代码  收藏代码
    1. <?php  
    2.   $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;  
    3.   $sticky=get_option('sticky_posts');  
    4.   $args=array(  
    5.     'cat'=>3,  
    6.     'caller_get_posts'=>1,  
    7.     'post__not_in' => $sticky,  
    8.     'paged'=>$paged,  
    9.   );  
    10.   query_posts($args);  
    11. ?>  

    时间参数

    检索特定时间段内发表的文章。

    • hour= -hour (时,-范围从0到23)
    • minute= – minute (分,-范围从0到60)
    • second= – second (秒,-范围从0到60)
    • day= – day of the month (日,-范围从1到31)
    • monthnum= – month number (月,-范围从1到12)
    • year= – 4 digit year (年,如2009)
    • w= – week of the year(一年中的第几周,-范围从0到53),使用 MySQL WEEK command Mode=1命令

    返回最近发表的文章

    Php代码  收藏代码
    1. $today = getdate();    
    2. query_posts('year=' .$today["year"] .'&monthnum=' .$today["mon"] .'&day=' .$today["mday"] );  

    返回12月20日发表的文章

    Php代码  收藏代码
    1. query_posts(monthnum=12&day=20' );  

    返回2009年3月1日到3月15日之间发表的文章

    Php代码  收藏代码
    1. <?php  
    2.   //based on Austin Matzko's code from wp-hackers email list  
    3.   function filter_where($where = '') {  
    4.     //posts for March 1 to March 15, 2009  
    5.     $where .= " AND post_date >= '2009-03-01' AND post_date < '2009-03-16'";  
    6.     return $where;  
    7.   }  
    8.   add_filter('posts_where', 'filter_where');  
    9.   query_posts($query_string);  
    10. ?>  

    返回最近30天内发表的文章

    Php代码  收藏代码
    1. <?php  
    2.   //based on Austin Matzko's code from wp-hackers email list  
    3.   function filter_where($where = '') {  
    4.     //posts in the last 30 days  
    5.     $where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";  
    6.     return $where;  
    7.   }  
    8.   add_filter('posts_where', 'filter_where');  
    9.   query_posts($query_string);  
    10. ?>  

    返回过去30天到过去60天内发表的文章

    Php代码  收藏代码
    1. <?php  
    2.   //based on Austin Matzko's code from wp-hackers email list  
    3.   function filter_where($where = '') {  
    4.     //posts 30 to 60 days old  
    5.     $where .= " AND post_date >= '" . date('Y-m-d', strtotime('-60 days')) . "'" . " AND post_date <= '" . date('Y-m-d', strtotime('-30 days')) . "'";  
    6.     return $where;  
    7.   }  
    8.   add_filter('posts_where', 'filter_where');  
    9.   query_posts($query_string);  
    10. ?>  

    分页参数

    • paged=2 ——显示点击“较早的日志”链接后出现在第二页中的文章
    • posts_per_page=10 —— 每页所显示的文章数量;若值为-1,显示所有文章。
    • order=ASC —— 按时间顺序显示文章,若值为DESC则按逆向时间顺序显示文章(默认)

     

    offset(偏移)参数

    通过offset参数,你可以移除或忽略正常情况下被查询集中的一篇或多篇初始文章。

    以下显示最近一篇文章之后的5篇文章:

    Php代码  收藏代码
    1. query_posts('posts_per_page=5&offset=1');  

    排序参数

    • orderby=author
    • orderby=date
    • orderby=category ——注意:该参数不能用于WordPress 2.8,可能已经被废止
    • orderby=title
    • orderby=modified
    • orderby=menu_order
    • orderby=parent
    • orderby=ID
    • orderby=rand
    • orderby=meta_value —— meta_key=some value语句也应出现在查询参数中
    • orderby=none – no order —— (新增于 WP 2.8)
    • orderby=comment_count ——(新增于 WP 2.9)

     

    顺序参数

    决定以升序或降序排列排序参数

    • order=ASC —— 升序,从最低值到最高值
    • order=DESC —— 降序,从最高值到最低值

     

    自定义字段参数

    根据自定义关键字或值检索文章(或页面)。

    • meta_key=
    • metavalue=
    • meta_compare= —— 用以测试metavalue=的操作符,默认值为 ‘=’,其它可能的值包括’!=’、 ‘>’、’>=’、 ‘<’或 ‘<=’ 。

    返回关键字为 ‘color’ 且值为’blue’的文章:

    Php代码  收藏代码
    1. query_posts('meta_key=color&metavalue=blue');  

    返回自定义字段关键字为’color’的文章,无论自定义字段值为何:

    Php代码  收藏代码
    1. query_posts('meta_key=color');  

    返回自定义字段值为’color’的文章,无论关键字为何:

    Php代码  收藏代码
    1. query_posts('metavalue=color');  

    返回自定义字段值为’green’的页面,无论自定义字段关键字为何:

    Php代码  收藏代码
    1. query_posts('post_type=page&metavalue=green');  

    返回自定义关键字为’color’、自定义字段值不为’blue’的文章和页面:

    Php代码  收藏代码
    1. query_posts('post_type=any&meta_key=color&meta_compare=!=&metavalue=blue');  

    返回自定义字段关键字为’miles’、自定义字段值小于等于22的文章。注意,字段值99会被看做大于字段值100,因为数据是以字符串形式而不是数字形式存储的。

    query_posts('meta_key=miles&meta_compare=<=&metavalue=22');

     

    联合参数

    你可能已经从上面有些例子中看出来了,可以用&符号连接不同参数,如:

    Php代码  收藏代码
    1. uery_posts('cat=3&year=2004');  

    显示主页上、当前月份发表的、隶属于分类13下的文章:

    Php代码  收藏代码
    1. if (is_home()) {  
    2.   query_posts($query_string . '&cat=13&monthnum=' . date('n',current_time('timestamp')));  
    3. }  

    在WP 2.3中,以下参数联合会返回同时属于分类1和分类3的两篇文章,以文章标题降序排列:

    Php代码  收藏代码
    1. query_posts(array('category__and'=>array(1,3),'posts_per_page'=>2,'orderby'=>title,'order'=>DESC));  

    在WP 2.3和WP 2.5中,以下参数联合本应返回属于分类1且带有“apples”标签的文章:

    Php代码  收藏代码
    1. query_posts('cat=1&tag=apples');  

    但由于一个bug,代码没能显示出正常结果。有一个解决办法:利用+号查找多个标签:

    Php代码  收藏代码
    1. query_posts('cat=1&tag=apples+apples');  

    这就显示出我们希望显示的结果了。

     

    使用技巧

    设置>阅读中的“博客页面最多显示”参数会影响你的查询结果,要覆盖设置>阅读中的设置,需要在标签中添加’posts_per_page’ 参数。例如:

    Php代码  收藏代码
    1. query_posts('category_name=The Category Name&posts_per_page=-1');  //returns ALL from the category  

    注意:query_posts函数会改写并取代页面的主查询。为谨慎起见,请不要将query_posts用作其它用途。

  • 相关阅读:
    通过hmc启动lpar的终端
    修复LVM手记
    通过VMLibrary在client partition上安装AIX全程实录
    【转】通过VIOS实现AIX系统的网络虚拟化
    rhel 6 启动流程分析(/etc/inittab)
    Linux中tty、pty、pts的概念区别
    Shell中while循环的done 后接一个重定向<
    搭建dns服务器时报错error: bind: address already in use
    关于 smit mktcpip 和smit chinet 的区别
    博客园博客停更(本博客收集本人于2018年之前的博客,2018年之后的博客统一发布在CSDN上)
  • 原文地址:https://www.cnblogs.com/jiahuaking/p/4198997.html
Copyright © 2011-2022 走看看