AnswerBun.com

Numeric pagination custom post type

WordPress Development Asked by skifast on August 1, 2020

i create a custom page to display loop of cpt with custom field.

I need to add a numberic pagination and i try with this code but not work.

Functions.php

function pagination_bar() {
    global $wp_query;

    $total_pages = $wp_query->max_num_pages;

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

        echo paginate_links(array(
            'base' => get_pagenum_link(1) . '%_%',
            'format' => '/page/%#%',
            'current' => $current_page,
            'total' => $total_pages,
        ));
    }
}

custompage.php

<!--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 )
);
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="<?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="<?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>
    <nav class="pagination">
        <?php pagination_bar(); ?>
    </nav>

<?php endwhile; wp_reset_query(); ?> 

Where is wrong?? Thanks

3 Answers

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="<?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="<?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;

Correct answer by Tunji on August 1, 2020

Make following changes in custompage.php file

<?php
$paged = ( get_query_var( 'page' ) ) ? get_query_var( 'page' ) : 1;
$args = array('post_type'=>'salmi','posts_per_page' => 15,'paged' => $paged);
$the_query = new WP_Query($args);
if ( $the_query->have_posts() ):
    while ( $the_query->have_posts() ) : $the_query->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="<?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="<?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( $the_query ); ?>
    </nav>
<?php wp_reset_postdata();
endif;
</div>

And in functions.php

function pagination_bar( $query_wp ) 
{
$pages = $query_wp->max_num_pages;
$big = 999999999; // need an unlikely integer
if ($pages > 1)
{
    $page_current = max(1, get_query_var('page'));
    echo paginate_links(array(
        'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
        'format' => '?paged=%#%',
        'current' => $page_current,
        'total' => $pages,
    ));
}
}

This works perfectly

Answered by kaustubh chaudhari on August 1, 2020

copy and pest in your function file

function pagination_bar( $query_wp ) 
{
    $pages = $query_wp->max_num_pages;
    $big = 999999999; // need an unlikely integer
    if ($pages > 1)
    {
        $page_current = max(1, get_query_var('paged'));
        echo paginate_links(array(
            'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
            'format' => '?paged=%#%',
            'current' => $page_current,
            'total' => $pages,
        ));
    }
}

**copy and pest code in your custompage.php file **

<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array('post_type'=>'salmi','posts_per_page' => 15,'paged' => $paged);
$the_query = new WP_Query($args);
if ( $the_query->have_posts() ):
    while ( $the_query->have_posts() ) : $the_query->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="<?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="<?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( $the_query ); ?>
    </nav>
<?php wp_reset_postdata();
endif;

Answered by NikHiL Gadhiya on August 1, 2020

Add your own answers!

Related Questions

Style WooCommerce Product Search Widget

0  Asked on December 19, 2020 by chirlet-deusa

     

Role – edit page but not publish

1  Asked on December 15, 2020 by miri-ull

   

How to create a breadcrumb for pages?

3  Asked on December 13, 2020 by msrajjc2

 

Weird issue with is_pages() with array

0  Asked on December 11, 2020 by birdseyedesign

 

WP_Query will not display draft posts

4  Asked on December 11, 2020 by ben-racicot

   

Flickity slider on WordPress: Modify behaviour

1  Asked on December 11, 2020 by vicdilu

 

Add the same content to multiple pages

1  Asked on December 10, 2020

   

Understanding WordPress(framework?) Web Development

2  Asked on December 10, 2020 by anuj-kaithwas

 

How to use value from modal tinymce windowManager?

1  Asked on December 10, 2020 by patrick-paul

   

wp_editor() renders outside wrapping div

0  Asked on December 9, 2020

 

WP REST API: Order posts by meta value (acf)?

2  Asked on December 9, 2020 by mauro-bringolf

       

How to incluide custom field in WooCommerce email using placeholder

0  Asked on December 8, 2020 by santiago-cuartas-rmrz

 

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP