WordPress

wp query – Wiele WP_Query z paginacją pokazuje zawartość strony/2 na pozostałych stronach

  • 23 grudnia, 2023
  • 5 min read
wp query – Wiele WP_Query z paginacją pokazuje zawartość strony/2 na pozostałych stronach


Przejrzałem wiele artykułów i przejrzałem prawie wszystkie zawarte tu pytania, ale nie udało mi się rozwiązać mojego problemu. Wyjaśnię więc szczegółowo mój scenariusz

Mój plik Index.php wygląda tak, który segreguje zawartość kategorii/paginacji:

<?php get_header(); ?>
<?php if(is_home() || is_front_page()){ ?>
    <?php get_template_part('templates/content','homepage'); ?>
    <?php } else if (is_home()){ ?>
        <?php get_template_part('templates/content','homepage'); ?> <!-- homepage  -->
    <?php } else if (is_page()){ ?>
        <?php get_template_part('templates/content','static'); ?> <!-- Static pages -->
    <?php } else if (is_single()){ ?>
        <?php get_template_part('templates/content','fullblog'); ?> <!-- Blog Detailed page -->
    <?php } else if (is_paged()){ ?>
        <?php get_template_part('templates/content','paged'); ?> <!-- Paginated content -->
    <?php } else { ?>
        <?php get_template_part('templates/content','blog'); ?> <!-- Category page -->
    <?php } ?>
<?php get_footer(); ?>

Otwierając dowolne menu kategorii w mojej witrynie WordPress, ładuje zawartość templates/content-blog.php. Zwróć uwagę na offset => 4 tutaj i wyświetla się poprawnie z paginacją:

<div class="section__one">
    <?php
    $the_query = new WP_Query( array(
        'category_name' => $category_name,
        'posts_per_page' => 4,
        'ignore_sticky_posts' => true,
    )); 
    ?>
    <div class="section__list">
        <?php if ( $the_query->have_posts() ) : ?>
        <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
        <a href="<?php the_permalink() ?>"><h1><?php echo substr(get_the_title(),0,150); ?></h1></a>
        <?php endwhile; ?>
        <?php wp_reset_postdata(); ?>
        <?php else : ?>
        <p><?php __('No News'); ?></p>
        <?php endif; ?>
    </div>
</div>
<?php 
    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    $the_query = new WP_Query( array(
        'category_name' => $category_name,
        'offset'      => 4,
        'orderby'     => 'post_date',
        'order'       => 'DESC',
        'post_type'   => 'post',
        'post_status' => 'publish',
        'posts_per_page' => 5,
        'paged' => $paged,
        'ignore_sticky_posts' => true,
    )); 
?>
<div class="category__posts">
    <?php if ( $the_query->have_posts() ) : ?>
    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    <div>
        <a href="<?php the_permalink() ?>"><h3><?php echo substr(get_the_title(),0,95); ?></h3></a>
    </div>
    <?php endwhile; ?>
    <div class="pagination">
    <?php 
    echo paginate_links( array(
        'base'         => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
        'total'        => $the_query->max_num_pages,
        'current'      => max( 1, get_query_var( 'paged' ) ),
        'format'       => '?paged=%#%',
        'show_all'     => false,
        'type'         => 'plain',
        'end_size'     => 2,
        'mid_size'     => 1,
        'prev_next'    => true,
        'prev_text'    => sprintf( '<i></i> %1$s', __( 'Newer Posts', 'text-domain' ) ),
        'next_text'    => sprintf( '%1$s <i></i>', __( 'Older Posts', 'text-domain' ) ),
        'add_args'     => false,
        'add_fragment' => '',
    ) );
    ?>
    </div>
    <?php wp_reset_postdata(); ?>
    <?php else : ?>
    <p><?php __('No News'); ?></p>
    <?php endif; ?>
</div>

Kliknięcie dowolnej strony/X powyżej templates/content-blog.phpładuje teraz zawartość z templates/content-paged.php

Niezależnie od tego, czy jest to Strona/2, Strona/3 czy Strona/X, na każdej stronie wyświetla się ta sama treść, chyba że ją usunę offset => 9.

Potrzebuję usunięcia pierwszych 9 postów ze strony/2, ponieważ są one zbędne, a następnie prawidłowego wyświetlenia pozostałych postów w treści podzielonej na strony. Jak mogę to rozwiązać?

<?php get_header(); ?>
<?php
    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    $query = new WP_Query( array(
        'offset' => 9,
        'posts_per_page' => 5,
        'paged' => $paged
    ) );
?>
<?php if ( $query->have_posts() ) : ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
    <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php endwhile; ?>
<div class="pagination">
    <?php 
        echo paginate_links( array(
            'base'         => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
            'total'        => $query->max_num_pages,
            'current'      => max( 1, get_query_var( 'paged' ) ),
            'format'       => '?paged=%#%',
            'show_all'     => false,
            'type'         => 'plain',
            'end_size'     => 2,
            'mid_size'     => 1,
            'prev_next'    => true,
            'prev_text'    => sprintf( '<i></i> %1$s', __( 'Newer Posts', 'text-domain' ) ),
            'next_text'    => sprintf( '%1$s <i></i>', __( 'Older Posts', 'text-domain' ) ),
            'add_args'     => false,
            'add_fragment' => '',
        ) );
    ?>
</div>
<?php wp_reset_postdata(); ?>
<?php else : ?>
    <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
<?php get_footer(); ?>


Źródło

Warto przeczytać!  Uzyskaj bieżącą tablicę użytkowników z ciągiem postów