最新消息: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)

filters - how to overwrite next_post_link

matteradmin10PV0评论

Is there any custom php code that lets me overwrite next_post_link so I can customize it to my liking? I can add code in functions.php

Right now it shows next post But I want to show something else

Is there any custom php code that lets me overwrite next_post_link so I can customize it to my liking? I can add code in functions.php

Right now it shows next post But I want to show something else

Share Improve this question edited Jan 14, 2019 at 14:40 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked Jan 14, 2019 at 14:32 Born vs. MeBorn vs. Me 135 bronze badges 3
  • What would you like it to show? – Krzysiek Dróżdż Commented Jan 14, 2019 at 14:39
  • Also, what's the context here? The function already has 2 arguments that allow you quite a bit of customisation at the moment of use, so a filter shouldn't be necessary. Are you working from a child theme? – Jacob Peattie Commented Jan 14, 2019 at 14:41
  • developer.wordpress/reference/hooks/adjacent_post_link – Jacob Peattie Commented Jan 14, 2019 at 14:41
Add a comment  | 

1 Answer 1

Reset to default 0

You can't easily change only the URL of that link, because there is no filter that will allow you to do that, but...

next_post_link uses get_next_post_link. There are no filters in any of these functions, but... get_next_post_link uses get_adjacent_post_link and inside of that function you can see {$adjacent}_post_link hook.

So now we can check docs for that hook. And then write some code:

function my_next_post_link($output, $format, $link, $post, $adjacent) {
    return '<a href="<MY LINK">MY LINK LABEL</a>';
}
add_filter( 'next_post_link', 'my_next_post_link', 10, 5 );

PS. Most likely you also have to add some conditions in there, so you don't change every link on your site and only the one you want to.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far