Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress: Check if there are previous posts before displaying link

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 &raquo;') ?></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?

like image 623
Chris Armstrong Avatar asked Apr 27 '10 17:04

Chris Armstrong


People also ask

What is PHP Posts_nav_link ()?

Displays the post pages link navigation for previous and next pages.

Which function displays Previous post link?

previous_post_link( string $format = '&laquo; %link', string $link = '%title', bool $in_same_term = false, int[]|string $excluded_terms = '', string $taxonomy = 'category' ) Displays the previous post link that is adjacent to the current post.


1 Answers

You can try something like this

<?php
    if($link = get_previous_posts_link()) {
        echo '<ul><li>'.$link.'</li></ul>';
?>

get_previous_posts_link returns null (falsy value) if there isn't any previous post.

like image 63
iBobo Avatar answered Sep 20 '22 15:09

iBobo