自定义模板页面2不起作用

时间:2019-03-11 作者:Sonjoy Datta

当我单击下一页时,第2页显示404错误页。第一页没问题。我已使用创建了此自定义帖子类型CPT UI 插件并从头开始构建主题。

    <?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();

1 个回复
SO网友:Gufran Hasan

请将您的查询修改为:

  $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>
将此函数放入functions.php 子主题的文件。

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

        }
    }

}

相关推荐

Count posts for pagination

我正在为一个网站分页<;上一页(页码)下一页>很简单,已经完成。但是现在我需要添加一个选择器来直接转到页面(例如:转到第7页),要这样做,我需要知道有多少页面,为此我需要计算在查询中找到了多少帖子。问题是这个网站有太多的帖子(>13.000),查询所有帖子都会减慢页面加载速度,这就像。。。10秒后页面才能加载。显然,这是不可接受的。分页解决了这个问题,因为一次只加载50或100篇文章,但我无法将它们全部计算在内。我可以在不加载的情况下统计某个查询中的帖子吗?或者我可以通过其他方式获得页数吗