pagination - Custom template Page 2 not working

admin2025-06-02  3

When I click Next page, page-2 showing 404 error page. Page one is OKAY. I have created this custom post type with CPT UI plugin and building the theme from scratch.

    <?php
/**
 * Template name: Art page
 */

get_header();

global $wp_query, $paged;

$artCurrentPage = get_query_var('paged');
$artPosts = new WP_Query(array(
        'post_type'      => 'art',
        'posts_per_page' => $wp_query->max_num_pages,
        'paged'          => $artCurrentPage
    ));
?>

    <div class="container">
        <div class="row">
            <div class="col-md-12">
                <div class="row">
                    <?php if ( $artPosts->have_posts() ) : ?>
                        <?php while ( $artPosts->have_posts() ) : $artPosts->the_post(); ?>
                        <div class="col-lg-4 col-md-6 col-sm-6">
                            <div class="trd-show-item mb-4">
                                <a class="trd-show-item-link" href="<?php the_permalink() ?>">
                                    <?php
                                        if ( has_post_thumbnail() ) {
                                            the_post_thumbnail( 'medium' );
                                        }
                                    ?>
                                </a>
                                <div class="trd-show-descr-under">
                                    <a class="trd-show-item-link-text" href="<?php the_permalink() ?>">
                                        <?php 
                                            echo the_title();
                                        ?>
                                    </a>
                                </div>
                            </div>
                        </div>
                        <?php endwhile; ?>
                        <div class="col-md-12">
                            <div class="posts-pagination">
                                <?php
                                    next_posts_link('Next Page', $artPosts->max_num_pages);
                                ?>
                            </div>
                        </div>
                    <?php else : ?>
                        <div class="col-12">
                            <div class="alert alert-info" role="alert"><?php esc_html_e( 'Sorry, we can\'t find any art gallery!', 'artist' ); ?></div>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>

<?php
get_footer();

When I click Next page, page-2 showing 404 error page. Page one is OKAY. I have created this custom post type with CPT UI plugin and building the theme from scratch.

    <?php
/**
 * Template name: Art page
 */

get_header();

global $wp_query, $paged;

$artCurrentPage = get_query_var('paged');
$artPosts = new WP_Query(array(
        'post_type'      => 'art',
        'posts_per_page' => $wp_query->max_num_pages,
        'paged'          => $artCurrentPage
    ));
?>

    <div class="container">
        <div class="row">
            <div class="col-md-12">
                <div class="row">
                    <?php if ( $artPosts->have_posts() ) : ?>
                        <?php while ( $artPosts->have_posts() ) : $artPosts->the_post(); ?>
                        <div class="col-lg-4 col-md-6 col-sm-6">
                            <div class="trd-show-item mb-4">
                                <a class="trd-show-item-link" href="<?php the_permalink() ?>">
                                    <?php
                                        if ( has_post_thumbnail() ) {
                                            the_post_thumbnail( 'medium' );
                                        }
                                    ?>
                                </a>
                                <div class="trd-show-descr-under">
                                    <a class="trd-show-item-link-text" href="<?php the_permalink() ?>">
                                        <?php 
                                            echo the_title();
                                        ?>
                                    </a>
                                </div>
                            </div>
                        </div>
                        <?php endwhile; ?>
                        <div class="col-md-12">
                            <div class="posts-pagination">
                                <?php
                                    next_posts_link('Next Page', $artPosts->max_num_pages);
                                ?>
                            </div>
                        </div>
                    <?php else : ?>
                        <div class="col-12">
                            <div class="alert alert-info" role="alert"><?php esc_html_e( 'Sorry, we can\'t find any art gallery!', 'artist' ); ?></div>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>

<?php
get_footer();
Share Improve this question asked Mar 11, 2019 at 9:20 Sonjoy DattaSonjoy Datta 1692 silver badges12 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Please modify your query as:

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

        $artPosts= new WP_Query( array(
                            'post_type'      => 'art',
                            'posts_per_page' => $posts_per_page,
                            'post_status'    => 'publish',
                            'orderby'        => 'title',
                            'order'          => 'DESC',
                            'paged'          => $paged
                            )); 

<?php if ( $artPosts->have_posts() ) : ?>
                        <?php while ( $artPosts->have_posts() ) : $artPosts->the_post(); ?>

// some code for fetching data

 <?php endwhile; ?>
<?php else : ?>
   <div class="col-12">
      <div class="alert alert-info" role="alert"><?php esc_html_e( 'Sorry, we can\'t find any art gallery!', 'artist' ); ?></div>
      </div>
<?php endif; ?>

<div id="pagination">
<?php wpex_pagination($artPosts); ?>
</div>

Put this function in functions.php file of your child-theme.

if ( ! function_exists( 'wpex_pagination' ) ) {

    function wpex_pagination( $query = '' ) {

        $prev_arrow = is_rtl() ? 'fa fa-angle-right' : 'fa fa-angle-left';
        $next_arrow = is_rtl() ? 'fa fa-angle-left' : 'fa fa-angle-right';

        global $wp_query;
        $wp_query = $query ? $query : $wp_query;
        $total = $wp_query->max_num_pages;
        $big = 999999999; // need an unlikely integer

        if ( $total > 1 )  {

             if ( ! $current_page = get_query_var( 'paged' ) ) {
                 $current_page = 1;
            }
            if ( get_option('permalink_structure') ) {
                $format = 'page/%#%/';
            } else {
                $format = '&paged=%#%';
            }

            echo paginate_links( array(
                'base'      => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
                'format'    => $format,
                'current'   => max( 1, get_query_var('paged') ),
                'total'     => $total,
                'mid_size'  => 3,
                'type'      => 'list',
                'prev_text' => '<i class="'. $prev_arrow .'"></i>',
                'next_text' => '<i class="'. $next_arrow .'"></i>',
            ) );

        }
    }

}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748820328a313997.html

最新回复(0)