• Ok, so I’m working on a theme and I want it to check if there are next posts to display. Something along these lines:

    <?php if (next_posts_link() || previous_posts_link()) { ?>
    <div class="alignleft"><?php next_posts_link('&laquo; Previous Entries') ?></div>
    <div class="alignright"><?php previous_posts_link('Next Entries &raquo;') ?></div>
    <?php } ?>

    Of course, this doesn’t work, because if next_posts_link does have something, it echoes it instead of returns it, and the whole thing stops.

    So, there is no get_previous_posts_link or anything like that, so what do I do? I can’t just have it display nothing in an empty div, because there are other elements I want to wrap these links in, but only if they exist. Understand what I mean? How can I check if there are other pages without echoing the page links?

    Thanks for any help

Viewing 7 replies - 1 through 7 (of 7 total)
  • 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('&nbsp;&nbsp;', '&laquo; Newer News', 'Older News &raquo;')) { posts_nav_link('&nbsp;&nbsp;', '&laquo; Newer News', 'Older News &raquo;'); echo ""; } ?>

    In action: https://www.finalgear.com/
    Not being shown: https://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.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Checking if there are next posts’ is closed to new replies.