I'm building my site with WordPress templates for granular control over how each of them looks.
I have my loop on my news page to pull in posts from, well, posts.
Working fine, or so I thought.
<?php
/*
Template Name: News
*/
?>
<?php get_header(); ?>
<div id="main-content">
<?php get_sidebar(); ?>
<?php query_posts ("posts_per_page=4"); ?>
<?php if (have_posts()) : while ( have_posts()) : the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
<div class="entry">
<?php the_content(); ?>
</div>
<?php include (TEMPLATEPATH . '/inc/meta.php' ); ?>
<div class="postmetadata">
<?php the_tags('Tags: ', ', ', '<br />'); ?>
Posted in <?php the_category(', ') ?> |
<?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?>
</div>
</div>
<?php endwhile; endif; ?>
<div class="navigation">
<div class="next-posts"><?php next_posts_link('« Older Entries') ?></div>
<div class="prev-posts"><?php previous_posts_link('Newer Entries »') ?></div>
</div>
</div> <!-- end div main-content -->
<?php get_footer(); ?>
The older/newer entries show up, but after clicking on them it just takes me to the same page/newest 4 posts.
How do I get the pagination working with template pages like this?
You need to add the paged
parameter to query_posts()
to paginate the results:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts('posts_per_page=3&paged=' . $paged);
?>
http://codex.wordpress.org/Pagination#Adding_the_.22paged.22_parameter_to_a_query
<div class="navigation">
<div class="next-posts"><?php next_posts_link('« Older Entries') ?></div>
<div class="prev-posts"><?php previous_posts_link('Newer Entries »') ?></div>
Just put this line in your page.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.