I
I
Ilya2017-06-20 13:40:38
CMS
Ilya, 2017-06-20 13:40:38

How to make pagination in wordpress page template?

I created a page template where I display a custom post type. You need to create a pagination - it is displayed, the required number of pages is shown, but when you go to another page, the URL changes, and the posts are displayed all the same from page 1:

$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
$args = array(
    'post_type' => 'reports',
    'posts_per_page' => 3,
    'tax_query' => array(
        array(
            'taxonomy' => 'reports_tax',
            'field' => 'slug',
            'terms' => $current_cat
        )
    ),
    'paged' => $paged
);
$query = new WP_Query($args);

if ($query -> have_posts() ) :
    while ($query -> have_posts() ) : $query -> the_post();
        get_template_part( 'template-parts/content', 'reports');
    endwhile;

    $big = 999999999; 
    echo paginate_links( array(
        'base'    => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
        'format'  => $format,
        'current' => max( 1, get_query_var('paged') ),
        'total'   => $query->max_num_pages,
        ) );
endif;

Answer the question

In order to leave comments, you need to log in

3 answer(s)
W
WordPress WooCommerce, 2017-06-20
@maxxannik

Never thought about such a question. Always used the basic WP loop.
For example, if the post type is report, then the base loop is supposed to open at reports. You can work with this.
If you need to change the output template, an endpoint of the /reports/mytmp/ type is made, and with this opening, another output template is generated.
Of course, you can nail pagination anywhere, but it's expensive. It is necessary to study all the possibilities well before getting involved in this adventure.
If you didn’t dissuade me, then dig towards the Rewrite Rule API https://truewp.ru/blog/wordpress/wp_rewrite.html
With due diligence, you can do everything, even scratch your foot behind your ear.

R
Ruslan, 2017-06-21
@rOOse

The custom post type has its own archive page which has its own template, why are you creating a separate page for them?

G
Grigore Rosca, 2017-07-18
@SeoAnatomy

Parination will not work on the page. It will only work on a taxonomy or archive page.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question