I tried the exact same ‘gee I hope this works!’ code. No go. I know what you mean – at the moment either the nav links are there when required or there is a big gap when there arent any other pages to navigate to. Need a function to ask wether there is a next or previous page to navigate to so the navigation space is only written when necessary.
Can you not do the following?
if( get_next_post() || get_previous_post() ) {
...
}
nope – i assume those functions u mention return true or false if the post has a next or previous post. What we want is a function that returns true or false if the current page of posts has a previous or next page of posts.
I see. Well, you could always grab a piece of code from the core and make your own custom functions.
I used the following on one of my blogs to not display the thing if there was gonna be no output. Maybe it’ll help you:
<?php if (posts_nav_link(' ', '« Newer News', 'Older News »')) { posts_nav_link(' ', '« Newer News', 'Older News »'); echo ""; } ?>
In action: http://www.finalgear.com/
Not being shown: http://www.finalgear.com/category/off-topic/
ok – got it working now:
I copied the two functions responsible for generating the links – next_posts_link and previous_posts_link and renamed them has_next_posts and has_previous_posts. Then replaced the bit where it echoes its output with –
return true
else return false
So now i can go –
if (has_next_posts || has_previous_posts) {
//put nav block in
}
I was confused a bit coz there is a function named ‘next_post_link’ and ‘next_posts_link’ and I was looking at the wrong one.
Thanks for the help viper and alphaoide.
I had to figure this one out too. But, I strongly did not want to modify the core files (because of the trouble that comes when updating). After way more work than should have been necessary, I came up with this solution, which I’m quite proud of…
I’ve written two functions and put them into a file called _extra_functions.php in the directory of my template…
This file has these two functions:
<?php
//functions tell whether there are previous or next 'pages' from the current page
//returns 0 if no 'page' exists, returns a number > 0 if 'page' does exist
//ob_ functions are used to suppress the previous_posts_link() and next_posts_link() from printing their output to the screen
function has_previous_posts() {
ob_start();
previous_posts_link();
$result = strlen(ob_get_contents());
ob_end_clean();
return $result;
}
function has_next_posts() {
ob_start();
next_posts_link();
$result = strlen(ob_get_contents());
ob_end_clean();
return $result;
}
?>
The “magic” is by using PHP’s ob_ functions to capture the output from the native wordpress functions and prevent them from printing to the screen.
Then, whereever I need to use it, I do this…
<?php include (TEMPLATEPATH . '/_extra_functions.php'); ?>
<?php if ( has_previous_posts() || has_next_posts() ) { // only show if previous or next links are needed ?>
//nav block goes here
<?php } //end if ?>
Thought I should post in case this could help someone else.