【问题标题】:How to include pagination in a Wordpress Custom Post Type Query如何在 Wordpress 自定义帖子类型查询中包含分页
【发布时间】:2017-06-03 22:47:34
【问题描述】:

我有以下代码:

<?php $the_query = new WP_Query( 'posts_per_page=30&post_type=phcl' ); ?>

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

<div class="col-xs-12 file">
    <a href="<?php echo $file; ?>" class="file-title" target="_blank">
        <i class="fa fa-angle-right" aria-hidden="true"></i> 
        <?php echo get_the_title(); ?>
    </a>
    <div class="file-description">
        <?php the_content(); ?>
    </div>
</div>
<?php endwhile; wp_reset_postdata(); ?>

我正在尝试使用paginate_links Wordpress 功能,但无论我把它放在哪里,我都无法让它工作。有人可以帮我解决这个问题吗?

【问题讨论】:

    标签: wordpress pagination


    【解决方案1】:

    试试下面的代码:

        $the_query = new WP_Query( array('posts_per_page'=>30,
                                     'post_type'=>'phcl',
                                     'paged' => get_query_var('paged') ? get_query_var('paged') : 1) 
                                ); 
                                ?>
    <?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
    <div class="col-xs-12 file">
    <a href="<?php the_permalink(); ?>" class="file-title" target="_blank">
    <i class="fa fa-angle-right" aria-hidden="true"></i> <?php echo get_the_title(); ?>
    </a>
    <div class="file-description"><?php the_content(); ?></div>
    </div>
    <?php
    endwhile;
    
    $big = 999999999; // need an unlikely integer
     echo paginate_links( array(
        'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
        'format' => '?paged=%#%',
        'current' => max( 1, get_query_var('paged') ),
        'total' => $the_query->max_num_pages
    ) );
    
    wp_reset_postdata();
    

    【讨论】:

    • 使用 代替
    • 这个解决方案有一个小错误。代码 'paged' => get_query_var('paged') ? get_query_var('paged') : 1) 不应有最后的括号。为了使分页能够正确运行,请从数字 1 之后删除右括号。
    • 查询分页不起作用,当我点击第二页时返回第一页帖子
    • @Jay 不,它没有
    【解决方案2】:

    使用新的WP_Query 查询循环时,将“total”参数设置为WP_Query 对象的max_num_pages 属性。

    自定义查询示例:

    <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    
    $args = array(
        'post_type'=>'post', // Your post type name
        'posts_per_page' => 6,
        'paged' => $paged,
    );
    
    $loop = new WP_Query( $args );
    if ( $loop->have_posts() ) {
        while ( $loop->have_posts() ) : $loop->the_post();
    
                 // YOUR CODE
    
        endwhile;
    
        $total_pages = $loop->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,
                'prev_text'    => __('« prev'),
                'next_text'    => __('next »'),
            ));
        }    
    }
    wp_reset_postdata();
    ?>
    

    适用于上述自定义查询的paginate_links 参数示例:

    更多参考请访问link

    【讨论】:

    • 感谢您分享此内容。它确实向我显示了分页。但是当我点击第 2 页的 URL 时,它仍然显示与第 1 页相同的内容。有什么想法吗?
    • @KhomNazid 你正在关注这个永久链接结构screencast.com/t/IeYH7bxY3 对吗?
    • @PurvikDhorajiya,没有。我正在关注%category%/%postname%,我们在自定义帖子之外的博客部分确实需要它。对于自定义帖子,我们依赖“Permalink Manager Lite”插件。我们的具体案例在这里详细介绍(现在我们的第 2 页实际上给出了 404):stackoverflow.com/questions/57301662/…
    • 这太完美了!! @PurvikDhorajiya
    • 这对我很有用,谢谢,但是,它总是将您带到页面顶部,就像您在不同页面之间导航一样,我希望它保持在同一位置并且只是似乎通过帖子部分进行分页。有任何想法吗?谢谢
    猜你喜欢
    • 2018-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-15
    • 2011-03-20
    • 2014-05-21
    • 1970-01-01
    相关资源
    最近更新 更多