$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>pagination - Custom template Page 2 not working|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

pagination - Custom template Page 2 not working

matteradmin9PV0评论

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>',
            ) );

        }
    }

}

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far