添加分页到我的自定义wordpress博客页面

时间:2016-06-03 06:52:19

标签: php wordpress pagination

我在word press中创建了一个自定义博客页面。我想为此页面添加分页。但我无法弄清楚如何做到这一点。

我的博客页面代码

<div id="container">
            <div id="content" role="main">

            <?php query_posts( array( 'posts_per_page' => 5 ) ); ?>


<?php if(have_posts()): while(have_posts()): the_post(); ?>
<div class="post-box">
<h1 class="entry-title"><?php the_title(); ?></h1>
<div class="entry-meta">
                        <?php twentyten_posted_on(); ?>
                    </div><!-- .entry-meta -->
<div class="entry-content"><?php the_excerpt(); ?></div>
<a class="more-button" href="<?php the_permalink(); ?>">Read More</a>
</div>
<?php endwhile; endif; ?>


            </div><!-- #content -->
        </div><!-- #container -->

2 个答案:

答案 0 :(得分:0)

在循环旁边添加分页,试试这个

 <div id="container">
            <div id="content" role="main">

            <?php query_posts( array( 'posts_per_page' => 5 ) ); ?>


<?php if(have_posts()): 

while(have_posts()): the_post(); ?>
<div class="post-box">
<h1 class="entry-title"><?php the_title(); ?></h1>
<div class="entry-meta">
                        <?php twentyten_posted_on(); ?>
                    </div><!-- .entry-meta -->
<div class="entry-content"><?php the_excerpt(); ?></div>
<a class="more-button" href="<?php the_permalink(); ?>">Read More</a>
</div>
<?php 

endwhile;


<div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>
<div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>
endif; 

?>


            </div><!-- #content -->
        </div><!-- #container -->

答案 1 :(得分:0)

您可以查看以下代码吗?

<div id="container">
    <div id="content" role="main">
    <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

    $args = array(
        'post_type'=>'post', // Your post type name
        'posts_per_page' => 5,
        'paged' => $paged,
    );

    $loop = new WP_Query( $args );
    if ( $loop->have_posts() ) {
        while ( $loop->have_posts() ) : $loop->the_post();
        ?>
            <div class="post-box">
                <h1 class="entry-title"><?php the_title(); ?></h1>
                <div class="entry-meta"><?php twentyten_posted_on(); ?></div><!-- .entry-meta -->
                <div class="entry-content"><?php the_excerpt(); ?></div>
                <a class="more-button" href="<?php the_permalink(); ?>">Read More</a>
            </div>
        <?php
        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();
    ?>
    </div><!-- #content -->
</div><!-- #container -->