向通过以下查询检索到的帖子添加分页

时间:2017-07-28 作者:Mounika

我正在尝试显示我的自定义帖子类型的所有已发布帖子,我编写了这个$query=新的WP\\U查询(数组(\'post\\u type\'=>\'x-video\',\'post\\u status\'=>\'publish\',\'posts\\u per\\u page\'=>-1))/循环代码/pagenavi();

这个查询一次检索所有已发布的帖子,我循环搜索结果并显示一个页面。

我需要的是,我希望它被分页为每页12篇文章。我该怎么做?

如果我将“posts\\u per\\u page”=>5,那么它只显示5篇文章,而不显示分页

2 个回复
SO网友:Navas Fazil
<?php 
$paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;

$news= new WP_Query(array(
    \'post_type\'=>\'post\',
    \'posts_per_page\' => 3,
    \'paged\' => $paged,
));

if($news->have_posts()) :
    while($news->have_posts())  : $news->the_post();
            the_title();
    endwhile;

    $total_pages = $news->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 »\'),
        ));
    }
    ?>    
<?php else :?>
<h3><?php _e(\'404 Error&#58; Not Found\', \'\'); ?></h3>
<?php endif; ?>
<?php wp_reset_postdata();?>
SO网友:Vishit
<?php $paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
                $args_blog = array(
                \'post_type\' => \'\'x-video\',
                \'paged\' => $paged,
                \'posts_per_page\' => 5
                );
                $query_blog = new WP_Query( $args_blog );
                if($query_blog->have_posts()) : while($query_blog->have_posts()) : $query_blog->the_post();
           ?>
                       <div class="pagination">
                           <?php
                    $big = 999999999; // need an unlikely integer
                    echo paginate_links( array(
                \'base\' => str_replace( $big, \'%#%\', esc_url( get_pagenum_link( $big ) ) ),
                \'format\' => \'?paged=%#%\',
                \'current\' => max( 1, get_query_var(\'paged\') ),
                \'prev_text\'          => __(\'Prev\'),
                \'next_text\'          => __(\'Next\'),
                \'total\' => $query_blog->max_num_pages
            ) );
            ?> 
                        </div>
                        <?php
                else :
                    get_template_part( \'content\', \'none\' );
                endif;
                ?>
结束

相关推荐

Pagination issue

我正在使用一个自定义帖子类型/自定义分类组合,我似乎无法使分页正常工作。我想在页面上添加一个无限卷轴,但我甚至无法显示分页以使无限卷轴工作。下面是我的循环:<?php $paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1; $args = array( \'post_type\' => \'shows\', // it\'s defa