2017-02-04 5 views
0

検索結果ページを変更して検索結果の数を10に変更する機能を変更しました。同じことをどうやって行うことができますか?タグ/用語ページの投稿数を10に変更

見つかり
function change_wp_search_size($query) { 
    if ($query->is_search) // Make sure it is a search page 
     $query->query_vars['posts_per_page'] = 10; // Change 10 to the number of posts you would like to show 
    return $query; // Return our modified query variables 
} 

add_filter('pre_get_posts', 'change_wp_search_size'); // Hook our custom function onto the request filter 

と、このコードを試してみました、それは問題があなたのis_tag()である

function main_query_mods($query) { 
    // check http://codex.wordpress.org/Conditional_Tags to play with other queries 
    if (!$query->is_main_query()) { 
     return; 
    } 
    if (is_tag()) { 
     $query->set('posts_per_page', 10); 
    } 
} 

add_action('pre_get_posts', 'main_query_mods'); 

答えて

0

仕事didntの。あなたはこれが好きだよ $query->is_tag()。あなたがあなたのif 声明の中で一緒にis_tag()is_search()を使用する必要が 両方tagsearchページの10によるポストを制限したい場合は

を更新し

function main_query_mods($query) { 
    if ($query->is_tag() && $query->is_main_query() && !is_admin()) { 
     $query->set('posts_per_page', 10); 
    } 
} 

add_action('pre_get_posts', 'main_query_mods'); 


function wh_tag_search_postCount($query) { 
    if (($query->is_tag() || $query->is_search()) && $query->is_main_query() && !is_admin()) { 
     $query->set('posts_per_page', 10); 
    } 
} 

add_action('pre_get_posts', 'wh_tag_search_postCount'); 


お店のページを除外したい場合、あなたは製品カテゴリアーカイブページのis_shop()is_product_category()を使用することができますv3の

を更新しました。

function wh_tag_search_postCount($query) { 
    //if WooCommerce is active 
    if (class_exists('WooCommerce')) { 
     //if current page is a shop page or product category page then dont do any thing 
     if (is_shop() || is_product_category()) 
      return; 
    } 
    if (($query->is_tag() || $query->is_search()) && $query->is_main_query() && !is_admin()) { 
     $query->set('posts_per_page', 10); 
    } 
} 

add_action('pre_get_posts', 'wh_tag_search_postCount'); 

すべてのコードがテストされ、機能しています。
希望すると便利です。

+0

これを試してみましたが、この新しい機能を追加すると、投稿カウントは10になりません。 – EmilyG

+0

@EmilyG:私は自分の答えを更新しました。 'wh_tag_search_postCount()'はあなたの投稿数の両方で動作します。 –

+0

検索では10個の投稿が正しく表示されていますが、タグページにはまだ5個の投稿しか表示されていません。 [リンク](https://www.baby-chick.com/tag/pregnancy/) – EmilyG

関連する問題