I'm using the following code to display a 'previous posts' link on my Wordpress blog.
<nav>
<ul>
<li><?php previous_posts_link('Newer Entries »') ?></li>
</ul
</nav>
Problem is, when there ARN'T any previous posts, while the link doesn't display, I still get
<nav>
<ul>
<li><</li>
</ul
</nav>
Printed out. Is there an if() statement I could wrap around it all so it checks if there are any previous posts, and only prints it out if there are?
You can try something like this
get_previous_posts_link
returns null (falsy value) if there isn't any previous post.for people checking this in 2013, get_previous_post has been depreciated.
http://codex.wordpress.org/Next_and_Previous_Links http://codex.wordpress.org/Function_Reference/previous_post
I used to use this :/
But now I use this :)
None of the answers worked for me. I solved it this way:
Just to be clear:
Colin's answer isn't correct in my opinion. get_previous_post is not deprecated, previous_post is.
http://codex.wordpress.org/Function_Reference/get_previous_post http://codex.wordpress.org/Function_Reference/previous_post
For me the use of get_next_post works still fine for me.