posts - Restrict next_post_link() to current category

admin2025-06-04  2

The Codex disagrees with itself, so I'm stumped.

On single.php I am trying to use next_post_link() to display a link to the next post, with custom link text, within the same category as the current post.

The Codex article on next_post_link() says the parameters are $format, $link, $in_same_term, $excluded_terms, and $taxonomy. Its specific example for my scenario is

<?php next_post_link( '%link', 'Next post in category', TRUE ); ?>

But when I use that exact code, no link is output at all. The rest of the Post fully renders, it's just missing the next post link HTML completely.

If I take out just the "TRUE" it outputs a link almost as desired:

<?php next_post_link( '%link', 'Next post in category' ); ?>

But it links to the next Post in any category, and I need to restrict it to the current category.

The Codex article on Next and Previous Links contradicts the article specifically on next_post_link(). It says the parameters are $format, $text, and $title. That would mean that you can't restrict the link to posts within the current category. Since the Code Reference on next_post_link() matches the Codex on next_post_link() that seems likely to be the most accurate.

The Codex disagrees with itself, so I'm stumped.

On single.php I am trying to use next_post_link() to display a link to the next post, with custom link text, within the same category as the current post.

The Codex article on next_post_link() says the parameters are $format, $link, $in_same_term, $excluded_terms, and $taxonomy. Its specific example for my scenario is

<?php next_post_link( '%link', 'Next post in category', TRUE ); ?>

But when I use that exact code, no link is output at all. The rest of the Post fully renders, it's just missing the next post link HTML completely.

If I take out just the "TRUE" it outputs a link almost as desired:

<?php next_post_link( '%link', 'Next post in category' ); ?>

But it links to the next Post in any category, and I need to restrict it to the current category.

The Codex article on Next and Previous Links contradicts the article specifically on next_post_link(). It says the parameters are $format, $text, and $title. That would mean that you can't restrict the link to posts within the current category. Since the Code Reference on next_post_link() matches the Codex on next_post_link() that seems likely to be the most accurate.

Share Improve this question asked Jan 24, 2019 at 15:16 WebElaineWebElaine 9,8491 gold badge17 silver badges28 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

OK, so there's a problem with Codes in this part, I guess.

The Codex article on Next and Previous Links contradicts the article specifically on next_post_link()

In that article you can clearly see, that it looked a little bit different. In the part describing next_post_link there is a note:

Deprecated: previous_post() and next_post(). Use: --> previous_post_link() and next_post_link() instead.

So most probably it is describing some old params...

On the other hand, from PHP point of view...

The official PHP manual says:

To specify a boolean literal, use the keywords TRUE or FALSE. Both are case-insensitive.

So true === TRUE and false === FALSE.

On the PSR-2 standard requires true, false and null to be in lower case.

Ok, this seems silly, but for some reason lowercasing the "true" solved the problem.

<?php next_post_link( '%link', 'Next post in category', true ); ?>

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748977094a315321.html

最新回复(0)