By default, the next_post_link() parameter in_same_term is set to ‘false’. Try editing the template and set this parameter to ‘true’.
You should create a Child theme to make your changes. If you do not, all of your changes will be lost if you update your theme.
I’m not using a template. I’m assuming I would need to create a functions.php file and add… what to it?
What theme are you using? If it is a free theme, someone can look at the code and give more specific advice. If it is a paid theme, you should contact the theme author.
I’m not using a theme. I’m using wordpress as a CMS for a website I designed myself.
If you did not follow the standard WP template hierarchy, I have no way of knowing what you need to do.
Hmm… Well here’s my code:
<h1>Nonfiction</h1>
<?php query_posts('category_name=nonfiction&showposts=4');
while (have_posts()) : the_post();
// do whatever you want
?>
<div id="post">
<div id="thumb">
<?php
preg_match_all("/(<img [^>]*>)/",get_the_content(),$matches,PREG_PATTERN_ORDER);
for( $i=0; isset($matches[1]) && $i < count($matches[1]); $i++ ) {
echo $beforeEachImage . $matches[1][$i] . $afterEachImage;}?>
</div>
<div id="pcontent">
<h2><?php the_title(); ?></h2>
<p><em><?php the_time('F jS, Y') ?></em></p>
<p><?php
ob_start();
the_content('Read the full post',true);
$postOutput = preg_replace('/<img[^>]+./','', ob_get_contents());
ob_end_clean();
echo $postOutput;
?></p>
</div>
</div>
<?php endwhile; ?>
<div id="more">
<p><?php next_post_link('%link', 'More Nonfiction', TRUE, $in_same_term = TRUE); ?></p>
I’ll admit that I’ve never looked at the standard template hierarchy, but–now that I have–I can’t seem to make heads or tails of my problem. To reiterate, I just want the page to display the older posts on the same page template that the original nonfiction posts appear. Why, when I click on “More Nonfiction” does it revert to the index.php template? How do I stop that from happening? I have tried making category-nonfiction.php and also category-4.php (the cat id) and also page-179.php (it seems to display the extra posts at ?p=179). What am I doing wrong?
To make pagination work, you must get the current page number and include it in your query_posts(), something like this (untested):
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') :
((get_query_var('page')) ? get_query_var('page') : 1 );
query_posts('category_name=nonfiction&showposts=4&paged=' . $paged);
Then, instead of using next_post_link(), which is intended to get the next single post, use next_posts_link() and previous_posts_link() for navigation.
It works! Thanks so much!