Wordpress自定义查询分页。我如何为此添加分页?

时间:2014-09-30 21:38:05

标签: php wordpress pagination

我目前通过查询从数据库获取所有帖子。包含的PHP代码是自定义Wordpress模板的一部分。我使用的是自定义元框:https://github.com/WebDevStudios/Custom-Metaboxes-and-Fields-for-WordPress

我需要做什么才能对每6个帖子进行分页?

         <?php 
            //get_template_part( 'content', 'page' ); 
            echo'<h2 class="section-header-dark">'.get_the_title().'</h2><hr>';

                //adjusting the query
                $args = array(
                    'post_type' => 'blog',
                    'posts_per_page' => -1,
                    'orderby' => 'menu_order',
                    'order' => ASC
                );

                // The Query
                $latest_post = new WP_Query( $args );
                // The Loop
                if ( $latest_post->have_posts() ) 
                {
                    while ( $latest_post->have_posts() ) 
                    {   
                        $latest_post->the_post();

                        $desc = wpautop( get_post_meta( get_the_ID(), '_cw_desc', true ) );

                        echo'<div class=""><h5 class="">'. get_the_title(). '</h5>';
                        echo $desc;
                        echo'<h6 class="news_date f_right"><i>'. get_the_date(). '</i></h6>';
                        echo'</div><hr>';

                    }
                } 
                else 
                {
                    // no posts do nothing
                }
                wp_reset_postdata();
        ?>

1 个答案:

答案 0 :(得分:0)

您需要设置'posts_per_page' => 6并将paged参数添加到查询中。

<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
  'posts_per_page' => 6,
  'paged' => $paged
);

?>

访问http://codex.wordpress.org/Pagination了解详情。

相关问题