Numeric pagination custom post type

You’re referencing the global $wp_query object in your function which you’ve reset using wp_reset_query().

You can resolve the pagination by passing your custom $loop WP_Query object to the function. I also changed wp_reset_query to wp_reset_postdata

Also you’re making the call to your pagination function in the while loop instead of after it.

Your function should be updated to:

function pagination_bar( $custom_query ) {

    $total_pages = $custom_query->max_num_pages;
    $big = 999999999; // need an unlikely integer

    if ($total_pages > 1){
        $current_page = max(1, get_query_var('paged'));

        echo paginate_links(array(
            'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
            'format' => '?paged=%#%',
            'current' => $current_page,
            'total' => $total_pages,
        ));
    }
}

and in your custompage.php file:

<!--Loop Salmi-->
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$loop = new WP_Query( array( 'post_type' => 'salmi',
        'posts_per_page' => 15,
        'paged'          => $paged )
);
if ( $loop->have_posts() ):
    while ( $loop->have_posts() ) : $loop->the_post(); ?>

    <!--Colonne Contenuto -->
    <div class="salmicpt">
        <div class="wpb_column vc_column_container td-pb-span8">
            <div class="titlecpt"><?php the_title(); ?></div>
        </div>
        <div class="wpb_column vc_column_container td-pb-span4">
            <?php if( get_field('audio_salmi') ): ?>
                <a href="https://wordpress.stackexchange.com/questions/250861/<?php the_field("audio_salmi'); ?>" ><img src="mysite.com/wp-content/uploads/cuffia-cpt-e1481533293805.png" alt="Ascolta" title="Ascolta" /></a>
            <?php endif; ?>
            <?php if( get_field('salmi_pdf') ): ?>
                <a href="https://wordpress.stackexchange.com/questions/250861/<?php the_field("salmi_pdf'); ?>" ><img src="mysite.com/wp-content/uploads/freccia-32.png" alt="Scarica il PDf" title="Scarica il PDF" /></a>
            <?php endif; ?>
        </div>
        <div style="clear:both"></div><hr class="style-one" />
    </div>
    <?php endwhile; ?>
    <nav class="pagination">
        <?php pagination_bar( $loop ); ?>
    </nav>
<?php wp_reset_postdata();
endif;

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)