Error wordpress paging (after second page) using custom template

Asked

Viewed 75 times

1

I made a custom page to show the blog post files, but would like to add pagination. When I make the pagination I can see only the first page, from the second page and all the next ones are not shown the posts. I am not used 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); ?>

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

Paging 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>";
    }

Error url 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 the version 5.2.2. of Wordpress.

1 answer

1


The solution was to change the way posts per page (posts 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>";
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.