如何在 WordPress 自定义帖子类型查询中包含分页

问题描述 投票:0回答:3

我有以下代码:

<?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
3个回答
24
投票

尝试下面的代码:

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

22
投票

使用新的 WP_Query 查询循环时,将“total”参数设置为

max_num_pages
对象的
WP_Query
属性。

自定义查询示例:

<?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,
    'post_status' => 'publish',
    'orderby' => 'date',
    'order' => 'DESC'
);

$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
参数示例:

欲了解更多参考,请访问此链接


1
投票
      Try This one, it worked for  me!
    
    <?php
                 $paged = get_query_var('paged');
                    
                $args = array(
                    'post_type' => 'blogs',
                    'paged' => $paged,
                    'orderby' => 'date',
                    'order' => 'ASC',
                    'posts_per_page' => 1,
                    'post_status' => 'publish',
                );
                $the_query = new WP_Query($args);
                if ($the_query->have_posts()) {
                    while ($the_query->have_posts()) {
                        $the_query->the_post();   
            ?>


             Your code...
        <?php
                        }

                    }
                   
     wp_reset_query();
  ?>

<div class="pagination">
<?php
                echo "<div class='fz-pagination'>" . paginate_links(array(
                    'total' => $the_query->max_num_pages,
                    'prev_text' => __('<div class="preious-page">Prev</div>'),
                    'next_text' => __('<div class="next-page">Next</div>')
                )) . "</div>";
            ?>
</div>



                   
© www.soinside.com 2019 - 2024. All rights reserved.