filters - how to overwrite next_post_link

admin2025-06-04  3

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.

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

最新回复(0)