0

Не работает пагинация постов

<div class="wrapper">
    <?php

        $posts = get_posts( array(
            'numberposts' => 10,
            'orderby'     => 'date',
            'order'       => 'DESC',
            'post_type'   => 'post',
            'suppress_filters' => true,
        ) );

            
        foreach($posts as $post) {
            setup_postdata($post); 
    ?>
            
    <a href="<?php the_permalink(); ?>" class="article-item">
        <div class="article-item__img">
            <?php the_post_thumbnail(); ?>
        </div>
        <div class="article-item__content">
            <h4><?php the_title(); ?></h4>
            <p><?php the_excerpt(); ?></p>
        </div>
    </a>

    <?php } wp_reset_postdata();  ?>
    <?php the_posts_pagination(); ?>
</div>
1

1 ответ 1

-1

Пагинация довольно часто не работает с get_posts(). Попробуйте переделать свой цикл на WP_Query:

$args = array(
        'posts_per_page' => 10,
        'orderby'     => 'date',
        'order'       => 'DESC',
        'post_type'   => 'post',
        'suppress_filters' => true );

$wp_query = new WP_Query( $args );
while ( $wp_query->have_posts() ) {
$wp_query->the_post();
//  ваш вывод
?>      
    <a href="<?php the_permalink(); ?>" 
 classs="article-item">
    <div class="article-item__img">
        <?php the_post_thumbnail(); ?>
    </div>
    <div class="article-item__content">
        <h4><?php the_title(); ?></h4>
        <p><?php the_excerpt(); ?></p>
    </div>
</a>

<?php }

// пагинация
the_post_pagination();

// вернем global $wp_query
wp_reset_postdata();
1
  • 2
    Откройте хоть раз код get_posts и не пишите чушь. 10 янв 2021 в 8:00

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.