$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>posts - Restrict next_post_link() to current category|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

posts - Restrict next_post_link() to current category

matteradmin9PV0评论

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 ); ?>

Post a comment

comment list (0)

  1. No comments so far