WordPress.org

Make WordPress Core

Opened 5 years ago

Closed 4 years ago

Last modified 4 years ago

#10867 closed enhancement (fixed)

next_post_link() and previous_post_link() should be allowed in the loop, even if is_single()

Reported by: jobjorn Owned by:
Milestone: 3.0 Priority: low
Severity: normal Version:
Component: Themes Keywords: has-patch 2nd-opinion
Focuses: Cc:

Description

I'm building a custom theme where the front page (example.org) is essentially an exact duplicate of a single post (example.org/2009/09/post-title/). Imagine my confusion when I copied the contents of single.php over to index.php and finding that the navigation links had disappeared.

I promptly tracked down the culprit, and it turned out to be a requirement in get_adjacent_post: if !is_single, it returns null.

I see no reason as to why these links should not be allowed everywhere in the loop. The codex page even states it is allowed in the loop, not telling of the extra limitations. Unless there is some reason of which I am not aware, I suggest that the attached patch be applied.

Attachments (2)

patch for not able to use next_post_link and previous_post_link on index page.diff (360 bytes) - added by jobjorn 5 years ago.
patch
10867.2.diff (1.4 KB) - added by ryan 4 years ago.

Download all attachments as: .zip

Change History (12)

comment:1 jobjorn5 years ago

  • Keywords has-patch added

comment:2 Denis-de-Bernardy4 years ago

  • Keywords 2nd-opinion added; themes the-loop removed
  • Milestone changed from Unassigned to 3.0

it could be argued they could be used for attachments, too (i.e. next gallery image)

comment:3 nacin4 years ago

I agree we should remove the !is_single() and is_attachment() checks, but could this potentially suddenly have links show up in themes?

comment:4 scribu4 years ago

Agree on removing is_attachment() too.

Yeah, links might pop up in 0.01% of themes, but I doubt there would be a backlash over the issue.

comment:5 nacin4 years ago

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

(In [13475]) Allow get_(previous|adjacent|next)_post() to be used on attachment and !single templates. props jobjorn, fixes #10867

comment:6 mdawaffe4 years ago

  • Resolution fixed deleted
  • Severity changed from trivial to normal
  • Status changed from closed to reopened

There are several themes (Journalist, Garland, presumably many others) that use previous_post_link()/next_post_link() on index.php without wrapping them in is_singular() conditionals.

Prior to 3.0, the links would not show unless the current page was is_singular(). Now they show up on all pages including the front page.

I like the 3.0 versions better (more flexible), but they break some themes.

Reopening.

Could leave as is.
Could revert.
Could add a new argument (or convert it them to wp_parse_args() based functions).
Could ...?

comment:7 ryan4 years ago

This also causes two pointless queries for every page because adjacent_posts_rel_link() is hooked to wp_head. Attached patch solves that given the current code.

ryan4 years ago

comment:8 ryan4 years ago

(In [14019]) Trigger adjacent_posts_rel_link() from wp_head only for single post pages to avoid unnecessary queries. see #10867

comment:9 mdawaffe4 years ago

  • Resolution set to fixed
  • Status changed from reopened to closed

MarkJaquith, nacin, ryan and I chatted about this OOB. We looked through themes in http://themes.svn.wordpress.org/ and decided that while this change breaks back compat, it doesn't affect too many themes. Most are either already wrapping in is_singular() conditionals or using them in single.php only.

We decided the back compat problems are worth the additional flexibility.

I guess we should sent something out to theme devs about this?

comment:10 mdawaffe4 years ago

Obviously, feel free to object/reopen/discuss :)

Note: See TracTickets for help on using tickets.