Conditional page/post navigation links in WordPress
Sunday, November 18th, 2007While creating a new theme for this site, I added some CSS styling around the page/post navigation links.
For example, in my index.php page, I have the following HTML/WordPress code:
<div class='navigation'>
<span class='older'><?php next_posts_link('« Older Entries') ?></span>
<span class='newer'><?php previous_posts_link('Newer Entries »') ?></span>
</div>
And the following CSS code:
.navigation {font-size:.7em; background:#353535; height:16px; margin:0 -16px 8px -16px; padding:8px 6px 0px;}
.navigation span {margin-top:-4px;}
.navigation .older {float:left;}
.navigation .newer {float:right;}
Everything looked great, until I brought up a page that did not have a previous or next link. The styled navigation div was still there, but there was nothing in it. So, I set out to conditionally display the navigation div if either a previous or next link existed.
