Is it possible to link to the next/prev posts in index.php with query_posts('cat=56'); before loop?

<?php query_posts('cat=56'); 
if (have_posts()) : while (have_posts()) : the_post(); ?>

next post title

<?php the_title(); ?>

previous post title

<?php endwhile; ?>
<?php endif; ?>

next/previous_post_link with TRUE doesn't work..

<?php next_post_link('&laquo; %link','%title', TRUE) ?>
<?php previous_post_link('%link &raquo;','%title', TRUE) ?>


I need to show in home page only selected post (selected category) with next/prev title of post in the same category (selected).enter image description here

In this image you can see the slider with caption. In this case, the next post is PIAZZALI PUBBLICI, but it does not belong to a selected category: the next image post in the slider it's another one.

  • Could you reproduce your code in full? It's not clear why you split in into loop and next/previous calls.
    – Rarst
    Commented Apr 24, 2014 at 16:20
  • You say it doesn't work. What happens? Commented Apr 24, 2014 at 16:20
  • it's a slide with post thumb caption with title of current and next/prev post @Rarst
    – brunly
    Commented Apr 24, 2014 at 16:28
  • the code show next/prev post without category selection @bestprogrammerintheworld
    – brunly
    Commented Apr 24, 2014 at 16:30
  • Category selection? The only thing the "link-functions" does it to include link and title of next and previous posts. How exactly do you want it to look like? Please give at least one example (update your question) Commented Apr 24, 2014 at 16:37

2 Answers 2


Naughty boy you, you should never use query_posts. Straight from the codex:

Note: This function isn't meant to be used by plugins or themes. As explained later, there are better, more performant options to alter the main query. query_posts() is overly simplistic and problematic way to modify main query of a page by replacing it with new instance of the query.

WP_Query should rather be used. I would do something like this

// set the "paged" parameter (use 'page' if the query is on a static front page)
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;

// the query
$the_query = new WP_Query( 'cat=56&paged=' . $paged ); 

<?php if ( $the_query->have_posts() ) : ?>

// the loop
while ( $the_query->have_posts() ) : $the_query->the_post(); 
<?php the_title(); ?>
<?php endwhile; ?>


// next_posts_link() usage with max_num_pages
next_posts_link( 'Older Entries', $the_query->max_num_pages );
previous_posts_link( 'Newer Entries' );


You can do that from your functions file using code like this:

add_action('loop_end', 'wpsites_nav_links', 25 );
function wpsites_nav_links() {
if( !is_single() ) 

previous_post_link('<span class="single-post-nav previous-post-link">%link</span>', '&lt;&lt; Previous Post in Category', TRUE);
next_post_link('<span class="single-post-nav next-post-link">%link</span>', 'Next Post in Category &gt;&gt;', TRUE);

Simply change loop_end to any other theme specific or WordPress hook or use a filter like the_content

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.