skip to Main Content

I have a custom post type that I want to loop with pagination. However it display correctly but whenever you click the pagination (1,2,3,4 next and prev) it always goes to the homepage. I use a shortcode to display on the page. Thanks in advance!


ob_start();
global $post;

    $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;

    $args = array (
        'post_type'     => 'ebooks-guides',
        'posts_per_page' => 4,
        'orderby'=> 'title',
        'order' => 'ASC',
        'paged' => $paged,
    
    );
    ?>
    <?php $the_query = new WP_Query( $args ); ?>
    <?php if (  $the_query->have_posts() )  : ?>
        <div class="ebooks-guides-container">
            <?php while ( $the_query->have_posts() ) : $the_query->the_post();?>
                <div class="ebooks-guides">
                    <figure>
                        <?php the_post_thumbnail('single-post-thumbnail'); ?>
                    </figure>
                    <div class="ebooks-guides-description">
                        <h2><?php echo get_the_title(); ?></h2>
                        <?php the_content(); ?>
                        <?php
                            $link = get_field('webinar_link');
                            if( $link ):
                                $link_url = $link['url'];
                                $link_title = $link['title'];
                                $link_target = $link['target'] ? $link['target'] : '_self';
                                ?>
                                <a class="cta-button" href="<?php echo esc_url( $link_url ); ?>" target="<?php echo esc_attr( $link_target ); ?>"><?php echo esc_html( $link_title ); ?></a>
                            <?php endif; ?>
                    </div>
                </div>
            <?php endwhile;?>
        <div class="pagination">
            <?php
            
                $big = 999999999; // need an unlikely integer
                echo paginate_links( array(
                    'base'      => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
                    'format'    => '?paged=%#%',
                    'current' => max( 1, get_query_var('paged') ),
                    'total'     => $the_query->max_num_pages,
                ) );
            ?>
        </div>
   
        
        </div>
    <?php wp_reset_postdata(); ?>
    <?php endif; ?>
    <?php
    return ob_get_clean();

2

Answers


  1. try this:

    $args = array (
        'post_type'     => 'ebooks-guides',
        'posts_per_page' => 4,
        'orderby'=> 'title',
        'order' => 'ASC',
        'paged' => get_query_var('paged'),
    
    );
    
    Login or Signup to reply.
  2. Add below function in your function.php file

    //  Pagination function
    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 add below code in your view area after loop complete.

    echo pagination_bar( $the_query);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search