Support » Fixing WordPress » evaluate if next_posts_link() will return results

  • Hi All,

    Wondering if anyone has a solution to use an if statement that looks at whether or not next_posts_link() will actually be populated or not. If there is a paged set of posts that next_posts_link() will allow the user to continue on, I want to put a header before the next and previous links.

    If next_posts_link() isn’t going to return anything, I don’t want to put that markup in. I thought this would be relatively simple, but so far no luck. Any advice would be very much appreciated!

Viewing 11 replies - 1 through 11 (of 11 total)
  • Have you had any luck finding a solution? I was wanting to do something similar but so far haven’t been able to figure out a solution. I have been trying to figure out how to write a custom function to include in my themes functions.php file but so far it is no good.

    This is what I need to know too. I want to display greyed out “Next Page” if there are no more pages left so that people can see that they are on the last page.

    Eric Martin

    (@emartin24)

    Sorry to dig up an old post, but I have the same question/issue.

    I want to know if there are any previous or next posts…what is the easiest way to do this?

    If there were a way to suppress the output of the calls like this:

    $older = next_posts_link('« Older Entries');
    $newer = previous_posts_link('Newer Entries »');
    $show_nav = FALSE;
    if (strlen($older) > 0 || strlen($newer) > 0) {
        $show_nav = TRUE;
    }

    That would be fine too. Basically, in addition to have_posts(), it would be nice to have a posts_count() or something like that.

    twistedtech

    (@twistedtech)

    I don’t have time to work on it right now but I do think I can make a solution. I ended up changing my theme so that it wasn’t needed but I will try to whip something up this weekend now that I know how to slap WP around a little better. If you don’t feel like waiting then just check out the code in ‘/wp-includes/link-template.php’. If you know some PHP it should be easy enough to create a custom function based off of the code they use in the previous_posts_link, next_post_link, and posts_nav_link functions.

    Eric Martin

    (@emartin24)

    @twistedtech – Thanks for the tip!

    I was able to throw something together that fit my needs…I’m thinking I might even create a plugin. I’ll post back here with my solution.

    -Eric

    twistedtech

    (@twistedtech)

    I really think that a plug-in is overkill when a couple of custom functions included in your theme’s functions.php file could be used instead. Especially since this is such a theme specific thing.

    Eric Martin

    (@emartin24)

    Good point =) I’ve posted my solution. I’m not sure if it is the right/best way to do it, but it works for me!

    twistedtech

    (@twistedtech)

    Well that looks like it would work. My thought was to just create a function that would return true or false based on if there was a next or previous link, but I didn’t have time to work on it this weekend. Not sure what this week is going to be like at work so I may have a chance to hack something up over the next few days. I think it is doable with a single function and 20 line of code or less, but I need to setup a test blog first so I don’t have to mess around with my live one.

    Eric Martin

    (@emartin24)

    Well, I thought about what you said and have added a different solution. Basically, I decided, for paged entries, to just check $wp_query->max_num_posts to determine if there was more than 1 page.

    I also removed the check for single posts, since, in my case, there will always be a next or previous post.

    Thanks again for your input.

    Here’s my solution. Pretty verbose, but I wanted to avoid the performance overhead of eval();*

    function dr_previous_posts_link() {
    	ob_start();
    	previous_posts_link('&laquo; <i>newer</i>');
    	$buffer = ob_get_contents();
    	ob_end_clean();
    	if(!empty($buffer)) echo "<h4 class=\"posts_nav\">$buffer</h4>";
    }
    
    function dr_next_posts_link() {
    	ob_start();
    	next_posts_link('&raquo; <i>older</i>');
    	$buffer = ob_get_contents();
    	ob_end_clean();
    	if(!empty($buffer)) echo "<h4 class=\"posts_nav\">$buffer</h4>";
    }

    *Using eval(); you could do something like this:

    function if_echo($code_string) {
    	ob_start();
    	eval($code_string);
    	$buffer = ob_get_contents();
    	ob_end_clean();
    	if(!empty($buffer)) echo $buffer;
    }

    With something like this in a template:

    <?php if_echo('next_posts_link()'); ?>

    On second thought, here’s a better solution:

    function dr_previous_posts_link($label = '', $pre = '', $post = '') {
    	ob_start();
    	previous_posts_link($label);
    	$buffer = ob_get_contents();
    	ob_end_clean();
    	if(!empty($buffer)) echo $pre,$buffer,$post;
    }
    
    function dr_next_posts_link($label = '', $pre = '', $post = '') {
    	ob_start();
    	next_posts_link($label);
    	$buffer = ob_get_contents();
    	ob_end_clean();
    	if(!empty($buffer)) echo $pre,$buffer,$post;
    }

    Then call them like this:

    <?php dr_previous_posts_link('&laquo; <i>newer</i>', '<h4 class="posts_nav">', '</h4>'); ?>
    <?php dr_next_posts_link('<i>older</i> &raquo;', '<h4 class="posts_nav">', '</h4>'); ?>
Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘evaluate if next_posts_link() will return results’ is closed to new replies.