自定义WP_QUERY和基础分页/POST_PER_PAGE的问题

时间:2016-11-23 作者:kevindeleon

在我的类别模板(category.php)中,我有以下代码:

<?php
// Setup custom loop -- we need to exclude featured posts from listing so they aren\'t repeated
$paged = get_query_var(\'paged\') ? absint(get_query_var( \'paged\' )) : 1;
$args = [
    \'post_type\' => \'post\',
    \'cat\'=> $current_category->cat_ID,
    \'post_status\' => \'publish\',
    \'paged\' => $paged,
    \'posts_per_page\' => $posts_per_page,
    \'orderby\' => \'date\',
    \'order\' => \'DESC\'
];

// If we have posts to exclude -- add that argument
if (!empty($featured_posts_to_exclude)) {
    $args[\'post__not_in\'] = $featured_posts_to_exclude;
}

$category_posts = new WP_Query($args);

if ( $category_posts->have_posts() ) :

    // Used in template part to vary content
    $loop_count = 0;

    /* Start the Loop */
    while ( $category_posts->have_posts() ) : $category_posts->the_post();
            // We use this instead of "get_template_parts" so that we can pass loop vars
            include(locate_template(\'template-parts/content-horz-ads.php\', false, false));
            $loop_count++;
    endwhile;

    // Add pagination
    im_numeric_posts_nav($category_posts);

    // Reset since we are using a custom loop.
    wp_reset_postdata();

else :

    get_template_part( \'template-parts/content\', \'none\' );
endif;
我的分页函数如下所示(我发布它是为了完整性,但无论posts\\u per\\u page设置为10,还是6,或者它是否是自定义循环,它都工作得非常好--它显示正确的链接和链接数):

function im_numeric_posts_nav($custom_query_object = null) {

    // If we\'re on a singular page, we don\'t need navigation
    if (is_singular()) {
        return;
    }

    // If a custom loop was passed in, use it...otherwise use global loop
    if ($custom_query_object !== null) {
        $wp_query = $custom_query_object;
    } else {
        global $wp_query;
    }

    /** Stop execution if there\'s only 1 page */
    if ($wp_query->max_num_pages <= 1) {
        return;
    }

    $paged = get_query_var(\'paged\') ? absint(get_query_var( \'paged\' )) : 1;
    $max = intval($wp_query->max_num_pages);

    /** Add current page to the array */
    if ($paged >= 1) {
        $links[] = $paged;
    }

    /** Add the pages around the current page to the array */
    if ($paged >= 3) {
        $links[] = $paged - 1;
        $links[] = $paged - 2;
    }

    if (($paged + 2) <= $max) {
        $links[] = $paged + 2;
        $links[] = $paged + 1;
    }

    echo \'<div class="pagination"><ul>\' . "\\n";

    /** Previous Post Link */
    if (get_previous_posts_link(\'&laquo;\')) {
        printf(\'<li>%s</li>\' . "\\n", get_previous_posts_link(\'&laquo;\'));
    }

    /** Link to first page, plus ellipses if necessary */
    if (!in_array( 1, $links)) {
        $class = 1 == $paged ? \' class="active"\' : \'\';

        printf(\'<li%s><a href="%s">%s</a></li>\' . "\\n", $class, esc_url(get_pagenum_link(1)), \'1\');

        if (!in_array(2, $links)) {
            echo \'<li>…</li>\';
        }
    }

    /** Link to current page, plus 2 pages in either direction if necessary */
    sort($links);
    foreach ((array) $links as $link) {
        $class = $paged == $link ? \' class="active"\' : \'\';
        printf(\'<li%s><a href="%s">%s</a></li>\' . "\\n", $class, esc_url(get_pagenum_link($link )), $link);
    }

    /** Link to last page, plus ellipses if necessary */
    if (!in_array($max, $links)) {
        if (!in_array($max - 1, $links)) {
            echo \'<li>…</li>\' . "\\n";
        }

        $class = $paged == $max ? \' class="active"\' : \'\';
        printf(\'<li%s><a href="%s">%s</a></li>\' . "\\n", $class, esc_url(get_pagenum_link($max)), $max);
    }

    /** Next Post Link */
    if (get_next_posts_link(\'&raquo;\', $max)) {
        printf(\'<li>%s</li>\' . "\\n", get_next_posts_link(\'&raquo;\', $max));
    }

    echo \'</ul></div>\' . "\\n";
}
在具有特色帖子的分类页面上,$posts_per_page 设置为6。

它只显示6篇文章,但分页查询仍然认为每页有10篇文章。

有214篇帖子,但应该只有35页,但如果我转到第22页之后的任何一页,我会得到404。这告诉我它仍在使用posts per page 在WordPress admin中的Reading settings.

如果我把它改为“6”--一切都是完美的玫瑰色。但是,我不希望将默认值设置为6。我想通过posts_per_page 自定义查询中的变量。

你知道为什么会这样,或者我这里有什么错吗?我真的很头疼。

1 个回复
SO网友:Jen

停止重写默认循环并使用pre_get_posts 相反像这样:

function wpse_247208_custom_category_query( $query ) {
    // Return early for any situation you aren\'t interested in.
    if ( is_admin() || ! $query->is_main_query() || !is_category() ) {
        return;
    }

    $query->set( \'posts_per_page\', 6 );

    // Don\'t use a global; create a function to populate this value
    // I just don\'t know how you created this value so I wanted to make sure 
    // it is represented.
    $featured_posts_to_exclude = get_featured_posts_to_exclude();
    // If we have posts to exclude -- add that argument
    if (!empty($featured_posts_to_exclude)) {
        $query->set(\'post__not_in\', $featured_posts_to_exclude);
    }
}
add_action( \'pre_get_posts\', \'hwl_home_pagesize\', 1 );

相关推荐

如果WP_QUERY中有数据,如何签入Functions.php?

我在主页上有两个不同的WP查询。我需要检查函数中的$home\\u查询条件。php然后,如果$home\\u query返回null,我想做些什么。我尝试了pre\\u get\\u posts操作,但我无法捕获$home\\u查询数据,而且它运行了两次,因为页面上有两个WP\\u查询。使用If$home\\u query返回null的分页方式,我想将页面重定向到404页面并获取404响应的代码。例如https://example.com/page/200/ 页码是200,但在页面中没有200页码,我想重