日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

说说WordPress的主查询函数-query_posts()

發布時間:2025/3/21 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 说说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代碼??
  • <?php??
  • ??if?(is_home())?{??
  • ????query_posts("cat=-3");??
  • ??}??
  • ?>??
  • ?

    你也可以多排除幾個分類。

    Php代碼??
  • <?php??
  • ??if?(is_home())?{??
  • ????query_posts("cat=-1,-2,-3");??
  • ??}??
  • ?>??
  • ?

    2.查詢指定文章

    用以下語句檢索某篇指定文章:

    Php代碼??
  • <?php??
  • //獲取ID值為5的文章??
  • query_posts('p=5');??
  • ?>??
  • ?

    如果你希望在查詢語句中使用Read More功能,請將全局變量$more設為0。

    Php代碼??
  • <?php??
  • //獲取ID值為5的頁面??
  • query_posts('p=5');??
  • ??
  • global?$more;??
  • //初始化$more??
  • $more?=?0;??
  • ??
  • //循環查詢到的結果??
  • while?(have_posts())?:?the_post();??
  • the_content('Read?the?full?post??');??
  • endwhile;??
  • ?>??
  • ?

    3.檢索指定頁面

    用以下語句檢索某篇指定頁面:

    Php代碼??
  • <?php??
  • query_posts('page_id=7');?//獲取頁面ID為7的頁面??
  • ?>??
  • ?

    或者

    Php代碼??
  • <?php??
  • query_posts('pagename=about');??
  • ?>??
  • ?

    檢索子頁面時,需要提供子頁面及其父頁面的別名,用斜線隔開兩者。例如:

    Php代碼??
  • <?php??
  • query_posts('pagename=parent/child');??
  • ?>??
  • ?

    上面都是采取 query_posts($query_string) 的形式來調用該函數,下面介紹另一種方法,用數組傳遞參數變量。

    Php代碼??
  • query_posts(array(??
  • ??'cat'?=>?22,??
  • ??'year'?=>?$current_year,??
  • ??'monthnum'?=>?$current_month,??
  • ??'order'?=>?'ASC',??
  • ));??
  • ?

    相比字符串方式,數組形式更加形象直觀,不容易出錯。

    下面整理一些經常要用到的參數,有些是我用過的,有些則沒有,算作歸納吧。

    分類參數

    只顯示特定分類下的文章。

    • cat —— 必須使用分類ID
    • category_name
    • category_and —— 必須使用分類ID
    • category_in —— 必須使用分類ID
    • category_not_in —— 必須使用分類ID

    根據ID顯示單個分類

    只顯示來自某一個分類目錄ID(以及該分類目錄下的子分類目錄)的文章:

    Php代碼??
  • query_posts('cat=4');??
  • ?

    根據分類名稱顯示單個分類

    只顯示來自某一個分類名稱下的文章:

    Php代碼??
  • query_posts('category_name=Staff?Home');??
  • ?

    根據ID顯示多個分類

    顯示來自若干指定分類目錄ID下的文章:

    Php代碼??
  • query_posts('cat=2,6,17,38');??
  • ?

    排除某一分類中的文章

    顯示除某一分類文章外的所有文章,被排除的分類ID以減號(’-')作為前綴。

    Php代碼??
  • query_posts('cat=-3');??
  • ?

    以上代碼刪除ID為3的分類中的文章。

    ?

    處理多個分類

    顯示隸屬于多個分類的文章。下面的代碼可展示同時屬于分類2和分類6的文章:

    Php代碼??
  • query_posts(array('category__and'?=>?array(2,6)));??
  • ?

    如果希望顯示分類2或分類6中的文章,可以使用上面介紹的cat,也可以使用category_in函數 (注意這里不會顯示分類下子分類中的文章) :

    Php代碼??
  • query_posts(array('category__in'?=>?array(2,6)));??
  • ?

    可以用下面這種方式排除多個分類中的文章:

    Php代碼??
  • 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代碼??
  • query_posts('tag=cooking');??
  • ?

    獲取若干標簽中任一標簽中的文章

    Php代碼??
  • query_posts('tag=bread+baking+recipe');??
  • ?

    多個標簽

    顯示同時屬于ID為37和47的標簽下的文章:

    Php代碼??
  • query_posts(array('tag__and'?=>?array(37,47));??
  • ?

    若要顯示ID為為37或47的標簽下的文章,可以使用tag參數,也可以用tag_in:

    Php代碼??
  • query_posts(array('tag__in'?=>?array(37,47));??
  • ?

    顯示的文章既不屬于標簽37,也不屬于標簽47:

    Php代碼??
  • 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代碼??
  • 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代碼??
  • $sticky=get_option('sticky_posts')?;??
  • query_posts('p='?.?$sticky[0]);??
  • ?

    Php代碼??
  • $args?=?array(??
  • 'posts_per_page'?=>?1,??
  • 'post__in'?=>?get_option('sticky_posts'),??
  • 'caller_get_posts'?=>?1??
  • );??
  • query_posts($args);??
  • ?

    注意:第二種方法只能返回最新發表的置頂文章;若當前無置頂文章,返回最新發表文章。

    ?

    返回第一篇置頂文章;若無,則不返回任何內容

    Php代碼??
  • $sticky?=?get_option('sticky_posts');??
  • $args?=?array(??
  • 'posts_per_page'?=>?1,??
  • 'post__in'?=>?$sticky,??
  • 'caller_get_posts'?=>?1??
  • );??
  • query_posts($args);??
  • if($sticky[0])?{??
  • //?insert?here?your?stuff...??
  • }??
  • ?

    從查詢中排除所有置頂文章

    Php代碼??
  • query_posts(array("post__not_in"?=>get_option("sticky_posts")));??
  • ?

    返回某一分類下所有文章,但不在文章列表上方顯示置頂文章。所有設為“置頂”的文章以正常順序(如日期順序)顯示

    Php代碼??
  • query_posts('caller_get_posts=1&posts_per_page=3&cat=6');??
  • ?

    返回某一分類下所有文章,完全不顯示置頂文章,保留分頁

    Php代碼??
  • <?php??
  • ??$paged?=?(get_query_var('paged'))???get_query_var('paged')?:?1;??
  • ??$sticky=get_option('sticky_posts');??
  • ??$args=array(??
  • ????'cat'=>3,??
  • ????'caller_get_posts'=>1,??
  • ????'post__not_in'?=>?$sticky,??
  • ????'paged'=>$paged,??
  • ??);??
  • ??query_posts($args);??
  • ?>??
  • ?

    時間參數

    檢索特定時間段內發表的文章。

    • 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代碼??
  • $today?=?getdate();????
  • query_posts('year='?.$today["year"]?.'&monthnum='?.$today["mon"]?.'&day='?.$today["mday"]?);??
  • ?

    返回12月20日發表的文章

    Php代碼??
  • query_posts(monthnum=12&day=20'?);??
  • ?

    返回2009年3月1日到3月15日之間發表的文章

    Php代碼??
  • <?php??
  • ??//based?on?Austin?Matzko's?code?from?wp-hackers?email?list??
  • ??function?filter_where($where?=?'')?{??
  • ????//posts?for?March?1?to?March?15,?2009??
  • ????$where?.=?"?AND?post_date?>=?'2009-03-01'?AND?post_date?<?'2009-03-16'";??
  • ????return?$where;??
  • ??}??
  • ??add_filter('posts_where',?'filter_where');??
  • ??query_posts($query_string);??
  • ?>??
  • ?

    返回最近30天內發表的文章

    Php代碼??
  • <?php??
  • ??//based?on?Austin?Matzko's?code?from?wp-hackers?email?list??
  • ??function?filter_where($where?=?'')?{??
  • ????//posts?in?the?last?30?days??
  • ????$where?.=?"?AND?post_date?>?'"?.?date('Y-m-d',?strtotime('-30?days'))?.?"'";??
  • ????return?$where;??
  • ??}??
  • ??add_filter('posts_where',?'filter_where');??
  • ??query_posts($query_string);??
  • ?>??
  • ?

    返回過去30天到過去60天內發表的文章

    Php代碼??
  • <?php??
  • ??//based?on?Austin?Matzko's?code?from?wp-hackers?email?list??
  • ??function?filter_where($where?=?'')?{??
  • ????//posts?30?to?60?days?old??
  • ????$where?.=?"?AND?post_date?>=?'"?.?date('Y-m-d',?strtotime('-60?days'))?.?"'"?.?"?AND?post_date?<=?'"?.?date('Y-m-d',?strtotime('-30?days'))?.?"'";??
  • ????return?$where;??
  • ??}??
  • ??add_filter('posts_where',?'filter_where');??
  • ??query_posts($query_string);??
  • ?>??
  • ?

    分頁參數

    • paged=2 ——顯示點擊“較早的日志”鏈接后出現在第二頁中的文章
    • posts_per_page=10 —— 每頁所顯示的文章數量;若值為-1,顯示所有文章。
    • order=ASC —— 按時間順序顯示文章,若值為DESC則按逆向時間順序顯示文章(默認)

    ?

    offset(偏移)參數

    通過offset參數,你可以移除或忽略正常情況下被查詢集中的一篇或多篇初始文章。

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

    Php代碼??
  • 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代碼??
  • query_posts('meta_key=color&metavalue=blue');??
  • ?

    返回自定義字段關鍵字為’color’的文章,無論自定義字段值為何:

    Php代碼??
  • query_posts('meta_key=color');??
  • ?

    返回自定義字段值為’color’的文章,無論關鍵字為何:

    Php代碼??
  • query_posts('metavalue=color');??
  • ?

    返回自定義字段值為’green’的頁面,無論自定義字段關鍵字為何:

    Php代碼??
  • query_posts('post_type=page&metavalue=green');??
  • ?

    返回自定義關鍵字為’color’、自定義字段值不為’blue’的文章和頁面:

    Php代碼??
  • 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代碼??
  • uery_posts('cat=3&year=2004');??
  • ?

    顯示主頁上、當前月份發表的、隸屬于分類13下的文章:

    Php代碼??
  • if?(is_home())?{??
  • ??query_posts($query_string?.?'&cat=13&monthnum='?.?date('n',current_time('timestamp')));??
  • }??
  • ?

    在WP 2.3中,以下參數聯合會返回同時屬于分類1和分類3的兩篇文章,以文章標題降序排列:

    Php代碼??
  • 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代碼??
  • query_posts('cat=1&tag=apples');??
  • ?

    但由于一個bug,代碼沒能顯示出正常結果。有一個解決辦法:利用+號查找多個標簽:

    Php代碼??
  • query_posts('cat=1&tag=apples+apples');??
  • ?

    這就顯示出我們希望顯示的結果了。

    ?

    使用技巧

    設置>閱讀中的“博客頁面最多顯示”參數會影響你的查詢結果,要覆蓋設置>閱讀中的設置,需要在標簽中添加’posts_per_page’ 參數。例如:

    Php代碼??
  • query_posts('category_name=The?Category?Name&posts_per_page=-1');??//returns?ALL?from?the?category??
  • ?

    ?

    注意:query_posts函數會改寫并取代頁面的主查詢。為謹慎起見,請不要將query_posts用作其它用途。

    ?

    ?

    來源: http://www.zuluo.net/2012/2012-01/wordpress-query_posts.html

    轉載于:https://www.cnblogs.com/kenshinobiy/p/7309233.html

    總結

    以上是生活随笔為你收集整理的说说WordPress的主查询函数-query_posts()的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。