Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Next and Previous Post links not working WordPress

  • SOLVED

Hi,

I can not get the NEXT and PREVIOUS links to work with WP_Query.

I am using Thesis and trying to setup a custom BLOG page.

I am calling a file called blog.php if page = blog

Here is my Thesis custom loop which checks for this condition;


$loopty_loop = new my_loops;

class my_loops extends thesis_custom_loop {
function page() {

if (is_page('blog')) {


include 'blog.php';


}

}

}


Inside the blog.php file I have the following code;

<?php $loop2 = new WP_Query( array ( 'order' => 'DESC', 'posts_per_page' => 4 ) ); ?>
<?php while ( $loop2->have_posts() ) : $loop2->the_post(); ?>


<?php the_title();?>
<?php the_content();?>

<?php endwhile; ?>

<?php next_posts_link('&laquo; Older Entries') ?>
<?php previous_posts_link('Newer Entries &raquo;') ?>

<?php wp_reset_postdata(); ?>


This works as far as pulling the first 4 posts into the page but the NEXT and PREVIOUS post links do not work.

I have tried countless different methods of writing this and have sampled many different snippets of code of people experiencing the same kind of issue but I can't get it to work so I am looking for some experienced suggestions on the matter.

Oddly enough the above code works on another blog (which is not using the Thesis theme) but I find it unlikely that it would be Thesis that's causing this issue.

Answers (3)

2011-09-19

Linda answers:

Hi, try this. :)

<?php
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query('showposts=4'.'&paged='.$paged);
?>



<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
do stuff here
<?php endwhile; ?>
<?php $wp_query = null; $wp_query = $temp;?>


Wordpressing comments:

This query pulls in the data, i.e. Title and Content but does not pull in the NEXT and PREVIOUS links to the page of posts.

<?php next_posts_link(); ?> = <strong>does not work</strong>
<?php previous_posts_link(); ?> = <strong>does not work</strong>

<?php next_posts_link('&laquo; Older Entries') ?> = <strong>does not work</strong>
<?php previous_posts_link('&laquo; Older Entries') ?> = <strong>does not work</strong>

<?php previous_post_link(); ?> = works <em><- but I don't need the previous post, I need the previous page of posts.
</em>

This is doing my head in haha....


Linda comments:

Odd, this is what I always use.

<?php previous_posts_link('&laquo; Previous') ?>
<?php next_posts_link('More &raquo;') ?>


Try putting it just before this :<?php $wp_query = null; $wp_query = $temp;?>

-Linda


Wordpressing comments:

That's where I have it placed but unfortunately it does not work.


Linda comments:

Ok, now it is doing my head in too. :)


Wordpressing comments:

Ok, here's what I have just noticed...

It works IF you are on the homepage eg. is_home() but does not work if you are on a page eg. is_page('blog')

Argh!


Linda comments:

Hi, try adding


global $paged;
global $wp_query;


above


$temp = $wp_query;


Wordpressing comments:

You are a legend, it WORKS!

Are you able to enlighten me why though? Is it because that information is not accessible unless you make the variable global on any other page that falls out side of the is_home() condition?


Linda comments:

Hi, I'm glad it worked! :) Here is a pretty cool flowchart someone made that helped me understand it better. Maybe it will help you too.

[[LINK href="http://wordpress.stackexchange.com/questions/1753/when-should-you-use-wp-query-vs-query-posts-vs-get-posts/1755#1755"]][[/LINK]]


Linda comments:

Dang, my link didn't show up. Let's try again...

[[LINK href="http://wordpress.stackexchange.com/questions/1753/when-should-you-use-wp-query-vs-query-posts-vs-get-posts/1755#1755"]]http://wordpress.stackexchange.com/questions/1753/when-should-you-use-wp-query-vs-query-posts-vs-get-posts/1755#1755[[/LINK]]


Wordpressing comments:

Thank you Linda, taking a read now.

Will award votes now also.

Much appreciated

2011-09-19

Kannan C answers:

Try this plugin http://wordpress.org/extend/plugins/wp-pagenavi/


Wordpressing comments:

This does not work either.


Kannan C comments:

As i didn't used this functions previously just i give it a try. The same issue happen to me also without adding the second parameter of the functions. So try this

<?php next_posts_link( '&laquo;' , '3' ); ?>
<?php previous_posts_link('&raquo;' , '3') ?>


Wordpressing comments:

Hi Kannan,

Thanks for your suggestion. Wp Pagenavi works when setting the global variables as Linda suggested and since I wish not to use a plugin in this instance I have to award the votes to her. But I appreciate you taking the time to respond.

Thank you.

2011-09-19

Grégory Viguier answers:

Is it for displaying a custom post type?
If so, I've made a plugin for that (still in beta but works fine so far) : http://wordpress.org/extend/plugins/sf-pages-for-custom-posts/


Wordpressing comments:

Hi Gregory,

In this instance it's not for a custom post type but I frequently create custom post types for clients and use pretty much the same code above or variants there of.

In any case your plug-in looks great, I'll keep that one in my bookmarks for future use as it's sometimes handy to allow clients to edit the amount of posts they wish to display without creating a custom-admin panel to dynamically modify those values.