0

我已经修改了我的搜索结果页面,其中包含将搜索结果计数更改为 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

找到并尝试了此代码,但它不起作用

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');
4

1 回答 1

0

问题出在你的is_tag(); 你必须这样 $query->is_tag()

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');


更新

如果你想在两个tagsearch页面上限制 10 个帖子,那么你必须在你的 声明中一起使用is_tag()和。is_search()if

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');


更新 v3

如果要排除商店页面,则可以使用is_shop()is_product_category()产品类别存档页面。

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');

所有代码都经过测试并且可以工作。
希望这可以帮助!

于 2017-02-04T17:51:25.613 回答