Make WordPress Core

Opened 18 years ago

Closed 14 years ago

#2834 closed enhancement (wontfix)

Cannot conditionalize previous_posts_link() or next_posts_link()

Reported by: paulschreiber's profile paulschreiber Owned by: pishmishy's profile pishmishy
Milestone: Priority: normal
Severity: normal Version: 2.2.3
Component: Template Keywords:
Focuses: Cc:


Since the functions previous_posts_link() and next_posts_link() print directly to the screen, you can't conditionalize around them, i.e. print tags before and after iff they are nonblank.

I have a patch that replaces next_posts_link() with next_posts_link_text(), which return()s the value instead of printing it. To preserve compatibility, next_posts_link() is a thin shim that calls print next_posts_link(). Similarly, next_posts() has been changed to be a thin shim around next_posts_text().

The same changes were made to previous_posts() and previous_posts_link().

Attachments (1)

template-functions-links.php.diff (2.7 KB) - added by paulschreiber 18 years ago.

Download all attachments as: .zip

Change History (10)

#1 @masquerade
18 years ago

This is unnecessary, as you can do this in the format tag of the existing link.

function previous_post_link( [$format='« %link', ...

Simply do something like the following:

previous_post_link('<strong><a href="%link" title="%title">%title</a></strong>');

#2 @Nazgul
17 years ago

  • Resolution set to worksforme
  • Status changed from new to closed

According to masquerade, this can already be done, so closing as worksforme

#3 @jrawle
17 years ago

  • Cc wordpress@… added
  • Resolution worksforme deleted
  • Status changed from closed to reopened
  • Type changed from defect to enhancement
  • Version changed from 2.0.3 to 2.2.3

It may be true that it's possible to format links as you wish. However, there are many other potential uses for a function that returns a value that can be used in conditional statements. For example, if the theme designer wants something completely different to happen when there is/isn't a "next" link.

There are also lots of posts in the forum requesting this functionality, for example:

#4 @foolswisdom
17 years ago

  • Milestone set to 2.5

#5 @pishmishy
16 years ago

  • Owner changed from anonymous to pishmishy
  • Status changed from reopened to new

How could a theme author tell whether there's a link or not? Anything that matches on the returned text is going to run into problems when you're running on a translated WordPress.

Your patch seems to be a lot of work to fix a more general issue in a specific place to achieve just one task. Could we simply add a $nolink (default to empty) parameter to adjacent_post_link() and have that value returned later in the function by

if ( !$post )
                return $nolink;

#6 @pishmishy
16 years ago

  • Milestone 2.6 deleted
  • Resolution set to wontfix
  • Status changed from new to closed

Proposed patch isn't ideal, no further feedback, so closing as won't fix.

#7 @jimisaacs
14 years ago

  • Resolution wontfix deleted
  • Status changed from closed to reopened
  • Version changed from 2.2.3 to 3.0

I think I understand what paulschreiber meant.

adjacent_post_link is one of the only functions in link-template without a return equivalent.

I have ran into this problem in dozens of sites and themes I have developed. Most of the time I need to wrap the next and prev navigation in an overall element to be able to style it more appropriately. In order to do this correctly, I need to see if there are actually links that can and will print. Whether it iss one, the other, or both.

If so, print the navigation, if not, don't print the navigation.

All this would be solved by adding get_adjacent_post_link(). Then calling that from adjacent_post_link.

Without this function I have to write a duplicate function over and over and over, changing one thing in one line.

'echo' to 'return'

#8 @jimisaacs
14 years ago

for instance this is an example of my most recent fix in a theme. Please pay no attention to the actual return value HTML, that is specific to the theme. What I am showing is what I as a developer am reduced to, if I don't want to rewrite the functions altogether.
This would be a lot easier if there were just some returns.


  • Gets the formatted post navigation for next and previous posts *
  • @return string HTML */

function mytheme_get_post_navigation( $format = '%link', $previous_text = '&larr; prev', $next_text = 'next &rarr;', $in_same_cat = false, $excluded_categories = ) {

Sorry, no other way to do this without completely duplicating WordPress function
ob_start(); previous_post_link($format, $previous_text, $in_same_cat, $excluded_categories ); $prev = ob_get_clean();
ob_start(); next_post_link($format, $next_text, $in_same_cat, $excluded_categories ); $next = ob_get_clean();
only set html if there are actually links available

return apply_filters( 'mytheme_post_navigation', ( $prev
$next ) ? '<nav class="post_navigation"><span class="alignleft previous">'.$prev.'</span> <span class="alignright next">'.$next.'</span></nav>' : );


#9 @ryan
14 years ago

  • Resolution set to wontfix
  • Status changed from reopened to closed
  • Version changed from 3.0 to 2.2.3

Could you open a new ticket for this against the "Future Release" milestone? This ticket is long dead and resurrecting zombies should be left to necromancers. :-)

Note: See TracTickets for help on using tickets.