简体   繁体   中英

Wordpress pagination error (after second page) using custom template

I made a custom page to show blog post, but would like to add pagination. When I do the pagination I can see only the first page, from the second page and all the next pages are not shown the post of the page. I am not using any plugin. Code below:

query

        $postStatus = 'publish';
        $postPerPage = 1;
        $pageType = is_page() ? 'page' : 'paged';
        $paged = (get_query_var($pageType) ? get_query_var($pageType) : 1);

        $args = array(
            'post_type'=> $postType,
            'post_status'=> $postStatus,
            'posts_per_page' => $postPerPage,
            'paged' => $paged
        );
        $query = new WP_Query($args); ?>

posts loop

             ?php if($query->have_posts() ) : while ($query->have_posts()) : $query->the_post(); ?>
                <?php if(has_post_thumbnail()) : 
                    $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );?>
                    <div class="col-sm-12 col-md-6 col-lg-4">
                        <div class="post">
                            <a class="link" href="<?php the_permalink(); ?>" alt="<?php the_title_attribute(); ?>">
                                <div class="post-thumbnail bg" style="background-image: url(<?php echo $url ?>"></div>
                                <p class="text"><?php the_title(); ?></p>
                            </a>
                        </div>
                    </div>
                <?php endif; ?>
            <?php 
            endwhile; 
            if(function_exists('pagination')) {
                pagination($query, $pageType); 
            }
            wp_reset_postdata(); 
            endif ?>

pagination function in functions.php

       function pagination($query, $pageType) {
        $big = 999999999;
        $maxNumPages = $query->max_num_pages;

        print_r($pageType);
        print_r($maxNumPages);

        echo "<nav class='pagination'>";
            echo paginate_links( array(
                'base'         => str_replace($big, '%#%', 
                 esc_url(get_pagenum_link($big))),
                'total'        => $maxNumPages,
                'current'      => max(1, get_query_var($pageType)),
                'format'       => '?' . $pageType . '=%#%',
                'show_all'     => false,
                'type'         => 'plain',
                'end_size'     => 2,
                'mid_size'     => 1,
                'prev_next'    => true,
                'prev_text'    => sprintf('<i></i> %1$s', __('«', 'text-domain' )),
                'next_text'    => sprintf('%1$s <i></i>', __('»', 'text-domain' )),
                'add_args'     => false,
                'add_fragment' => '',
            ));
        echo "</nav>";
    }

Url page my-url/wordpress/page/2/

I've done several searches and checked the code and could not find the source of this error, I'm using version 5.2.2. from Wordpress.

The solution was change the way to set the post per page:

query

$postStatus = 'publish';
    $postPerPage = 1;
    $pageType = is_page() ? 'page' : 'paged';
    $paged = (get_query_var($pageType) ? get_query_var($pageType) : 1);

    $args = array(
        'post_type'=> $postType,
        'post_status'=> $postStatus,
        'paged' => $paged
    );
    $query = new WP_Query($args); ?>

functions.php

function setPostsPerPage($query) {
    $query->set('posts_per_page', 6);

    if (!is_admin() && $query->is_main_query()) {
      if(is_home()){
        $query->set('posts_per_page', 1);
      }
    }
}

add_action('pre_get_posts', 'setPostsPerPage');

function pagination($query, $pageType) {
    $big = 999999999;
    $maxNumPages = $query->max_num_pages;

    echo "<nav class='pagination'>";
        echo paginate_links( array(
            'base'         => str_replace($big, '%#%', esc_url(get_pagenum_link($big))),
            'total'        => $maxNumPages,
            'current'      => max(1, get_query_var($pageType)),
            'format'       => '?' . $pageType . '=%#%',
            'show_all'     => false,
            'type'         => 'plain',
            'end_size'     => 2,
            'mid_size'     => 1,
            'prev_next'    => true,
            'prev_text'    => sprintf('<i></i> %1$s', __('«', 'text-domain' )),
            'next_text'    => sprintf('%1$s <i></i>', __('»', 'text-domain' )),
            'add_args'     => false,
            'add_fragment' => '',
        ));
    echo "</nav>";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM