WordPress

zapytanie wp — Problem z niestandardowym WP_Query i podstawową paginacją/posts_per_page

  • 23 listopada, 2016
  • 4 min read
zapytanie wp — Problem z niestandardowym WP_Query i podstawową paginacją/posts_per_page


W moim szablonie kategorii (category.php) mam następujący kod:

<?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;

Moja funkcja paginacji jest następująca (publikuję ją ze względu na kompletność, ale działa całkowicie dobrze, niezależnie od tego, czy posts_per_page jest ustawione na 10, czy 6, czy też jest to niestandardowa pętla, czy nie – wyświetla poprawne linki i numer linków):

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=" . "\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=" . "\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=" . "\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";
}

Na stronach kategorii z polecanymi postami, $posts_per_page jest ustawiony na 6.

Warto przeczytać!  przepisywanie adresów URL - Przepisz zagnieżdżone adresy URL dla niestandardowego typu postu

Pokazuje tylko 6 postów, ale zapytanie o paginację nadal uważa, że ​​​​na stronie jest 10 postów.

Jest 214 postów, ale powinno być tylko 35 stron, ale jeśli przejdę na jakąkolwiek stronę po stronie 22, dostanę 404. Co mówi mi, że nadal używa posts per page który jest ustawiony w panelu administracyjnym WordPress pod Reading settings.

Jeśli zmienię to na „6” – wszystko będzie idealnie różowe. Jednak nie chcę, aby ta wartość domyślna była ustawiona na 6. Chcę mieć możliwość ustawienia tego za pomocą posts_per_page var w niestandardowym zapytaniu.

Masz jakiś pomysł, dlaczego tak się dzieje lub co tu jest nie tak? Naprawdę wali mi to w głowę.


Źródło