• Hi all,

    I integrated wordpress into one of my websites to use it as a content management system. Now I have the following problem:

    There are two categories, CAT1 and CAT2. Both categories contain images and details from art. Now I want to add a PREVIOUS and NEXT button to each post of a category, so you can switch between the different pieces of art.

    CAT1 uses index.php

    <?php
    	$lastposts = get_posts('numberposts=1&offset=0&category=3&orderby=post_date&order=ASC');
    
    	foreach($lastposts as $post) :
        		setup_postdata($post);
    		?>
    
    	<?php the_content(); ?>
    
    	<ul>
    	<li class="voorbeeld-vorige"><a href="<?php next_post_link('%link', 'Next post in category', TRUE, '13'); ?>"><span>vorige foto</a></li>
    	<li class="voorbeeld-volgende"><a href=" <?php previous_post_link('%link', 'Previous in category', TRUE, '13'); ?> "><span>volgende foto</span></a></li>
    	</ul>
    
    	<?php endforeach; ?>

    But this doesn’t work. It does display the next and previous link, but they don’t link to the next or previous post of the category.

    Does anyone know how to fix this problem?

    Thanks in advance!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The problem here is next/previous_post_link is intended for display on single post pages, which is not what index.php normally is handling.

    First, add this right after setup_postdata() (but within the ?> tag):

    $wp_query->is_single = true;

    That will fool WordPress into thinking we’re on a single post page. Also, after your loop ends, insert:

    <?php $wp_query->is_single = false; ?>

    to make the change temporary and avoid screwing up anything.

    Second, your use of next/previous_post_link is incorrect. These generate the <a> elements and text links, rather than just output urls for the next/previous posts.

    (The original post is fairly old now, but since the thread was pulled up from the ether it seemed a good idea to respond in case someone else stumbles across the thread looking for a similar answer.)

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Next/Previous post of category’ is closed to new replies.