tags:

views:

33

answers:

2

I am building a wordpress theme I want to have the next and previous DIV only appear IF their is a next and previous button available.

If there is 10 posts and the display limit is 10 posts I don't want an empty div. However when there are 20 posts and the display limit is 10 I want the next and previous buttons to show in there own div.

I got this far and realized that posts_nav_link() doesn't return null.

function next_previous_div(){
print '<!-- ';
$output = posts_nav_link();
print " -->\n";
if ($output != null) 
    {   
        echo '<div class="float post" style="text-align:center">';
        echo posts_nav_link();
        echo '</div><!-- end post-->';
    };
};
A: 

I think the closest you will get without custom scripting will be...

if (!empty(get_posts_nav_link()) {
   echo '<div class="float post" style="text-align:center">';
   echo posts_nav_link();
   echo '</div><!-- end post-->';
}
Cags
+2  A: 

There are a few different ways you could test if you'll have a previous/next link:

Test $wp_query->max_num_pages

Get a global reference to the $wp_query object and see if it's greater than 1. If yes, you've got paging links:

global $wp_query;
if($wp_query->max_num_pages > 1){
   posts_nav_link();
}

Capture the output of posts_nav_link()

This function echos by default, which is why your null test isn't working. You need to capture the echoed output and then test:

// Capture the echoed output in the $links variable and test on it
ob_start();
posts_nav_link();
$links = ob_get_clean();

if(strlen($links) > 0){
    echo $links;
}
Pat
Thanks I used the second, worked perfectly!
ThomasReggi
Glad to hear - when I first started with WordPress, the functions that echoed automatically got me a few times too.
Pat