亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

WP搜索頁面帖子計數根據帖子類型有條件顯示

WP搜索頁面帖子計數根據帖子類型有條件顯示

PHP
梵蒂岡之花 2023-12-15 15:15:44
我正在嘗試根據帖子類型在搜索頁面上實現帖子數量(帖子計數)的條件顯示。我正在尋找這樣的東西:add_action( 'pre_get_posts', function ( $query ) {  if( ! is_admin() && $query->is_search() && $query->is_main_query() && is_post_type == 'MY_POST_TYPE' ) {    $query->set( 'posts_per_page', 1 );  };} );我可以在哪里使用is_post_type == 'MY_POST_TYPE'來指定帖子類型...我一直在四處尋找但無法找到答案...它 39;有點麻煩,因為它聽起來像是一個基本功能...有什么想法嗎?編輯 1: 像這樣,根據帖子類型顯示不同數量的帖子add_action( 'pre_get_posts', function ( $query ) {if( ! is_admin() && $query->is_search() && $query->is_main_query() && is_post_type == 'MY_POST_TYPE_1' ) {$query->set( 'posts_per_page', 1 );} elseif ( ! is_admin() && $query->is_search() && $query->is_main_query() && is_post_type == 'MY_POST_TYPE_2' ) {$query->set( 'posts_per_page', 10 );};} );  
查看完整描述

1 回答

?
慕的地8271018

TA貢獻1796條經驗 獲得超4個贊

要根據查詢 CPT 添加其他條件查詢參數,我們可以使用 pre_get_posts 掛鉤來執行此操作,就像您所做的那樣。


檢查查詢是否針對 CPT

我們將創建一個自定義函數,該函數將掛鉤此操作并檢查 $query 是否包含 CPT,如果包含,則添加額外的參數。正如您所想的那樣,這將起作用:


add_action( 'pre_get_posts', 'add_cpt_query_args');


function add_cpt_query_args( $query ) {

    // If this isn't the main search query on the site, then do nothing.

    if( is_admin() || !$query->is_search() || !$query->is_main_query() )

        return;

    // Check if the query is for any of our CPTs

    if (in_array ( $query->get( post_type'), array('YOUR_CPT_SLUG', 'ANOTHER_CPT') ) ){

        // set the args for this CPT

        $query->set( 'posts_per_page', 1 );

        $query->set( 'orderby', 'rand' );

    }

}

檢查多個 CPT 和不同的查詢參數

由于您想要對多個 CPT 執行此操作并為它們提供不同的參數(例如 posts_per_page),因此您必須為每個 CPT 添加單獨的 if在上面的代碼中。


更靈活且可維護的方法 執行此操作是使用數組將每個 CPT 的所有查詢參數信息保存在數組中。這意味著:


我們可以簡單地循環遍歷它以使用添加查詢參數 - 無需添加額外的if檢查每個要檢查的 CPT

我們可以根據需要為每個 CPT 添加任意數量的查詢參數(例如 pages_per_post、order_by 等),每個 CPT 可以有不同的查詢參數args,并且不必將它們硬編碼到代碼中,或者手動檢查我們是否有它們的值。

我們可以使用此代碼來做到這一點 - 下面有對其工作原理的完整說明 (注意 - 這未經測試,但基本邏輯已經存在?。?/p>


add_action( 'pre_get_posts', 'add_cpt_query_args');


function add_cpt_query_args( $query ) {


    // If this isn't the main search query on the site, then do nothing.

    if( is_admin() || !$query->is_search() || !$query->is_main_query() )

        return;


    // As you want to do this for multiple CPTS, a handy way to do this is set up all the info in an array

    $CPT_args['qcm'] = array('posts_per_page'=> 1, 'orderby' => 'rand');

    $CPT_args['anotherCPT'] = array('posts_per_page'=> 5, 'orderby' => 'date', 'order => 'ASC'');


    // Now check each CPT in our array to see if this query is for it

    foreach ($CPT_args as $cpt => $query_args ){

        if (in_array ( $query->get( post_type'), array($cpt) ) ){


            // this query is for a CPT in our array, so add our query args to the query

            foreach ($query_args as $arg => $value )

                $query->set( $arg, $value );

         }

    }

}

這是如何運作的

1。檢查這是否是主站點上的主要搜索查詢 - 如果不是(并且我們沒有其他條件),最干凈的方法是在此時簡單地結束該函數,那么我們就不會嘗試在 中保留大量代碼if


if( is_admin() || !$query->is_search() || !$query->is_main_query() )

    return;

2.在數組中設置 CPT 參數 - 因為您想對多個 CPT 執行此操作,最簡單的方法是將所有信息存儲在一個數組中,我們可以在數組中簡單地使用該數組循環。


在此示例中,我們將使用一個名為 $CPT_args 的數組,并為我們要檢查的每個 CPT 添加一個條目,并以 CPT 名稱作為鍵。一個>

我們將使用另一個數組作為值,這將包含您想要添加到此 CPT $query 的所有參數,例如

    $CPT_args['qcm'] = array('posts_per_page'=> 1, 'orderby' => 'rand');

使用此數組可將所有參數信息保存在一個位置,并且可以輕松添加或刪除更多 CPT 條件,而不會影響影響查詢的主代碼。


3.檢查 $query 是否適用于任何 CPTS,方法是循環遍歷我們的數組并根據我們的鍵檢查查詢 post_type $CPT_args數組:


foreach ($CPT_args as $cpt => $query_args ){

    if (in_array ( $query->get( post_type'), array($cpt) ) ){

        ... 

    }

}


4。如果此查詢針對的是 CPT,請將 CPT 的查詢參數添加到 $query。我們可以簡單地查看我們的參數數組并將它們添加到 $query。


    foreach ($queryargs as $arg => $value )

        $query->set( $arg, $value );


查看完整回答
反對 回復 2023-12-15
  • 1 回答
  • 0 關注
  • 161 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號