3

It is in Wordpress, Divi. It seemed to me a simple task but somehow I haven't managed to get the proper result spending tons of hours with coding and testing. Currently I'm working on a website, where there are posts and I intend to give the possibility the user to jump to the adjacent posts with a simple link pointing to them. I've tried to put them in functions.php, then in single.php but the same result... Nothing. I checked it via Wordpress debug.log, it shows that the functions return no value at all. Note that I mention debug.log, because I forwarded outputs over there.

add_filter( 'the_content', 'post_navigation', 10, 2 );
function post_navigation($content) {
    $post = get_post();
    $post_type = get_post_type( $post );

    if ( $post_type == 'post' ) {
        $next_p = get_previous_posts_link();
        $content = $content . $next_p;

        return $content;
    } else {
        return $content;    
    }

}

It has no proper formatted output, but first I just wanted to get any result. Note: debug.log shows that the the_content hook has been 2 times called at a simple post displaying second times with the content:

You are being timed-out out due to inactivity. Please choose to stay signed in or to logoff.</p>
<p>Otherwise, you will be logged off automatically.</p>

Any help would be highly appriciated!

4

Warning Edit: Mark Kaplun's answer brings up a very good point - this will modify your content in unexpected ways. Say, for instance, you have another application that reads this post content from the WordPress API - that content will also have these links which is probably not what you want. You should really be applying these methods in your theme templates instead of appending this information to the content itself. You could possibly use sanity checks such as checking against the global $pagenow variable, or other methods, to ensure you only do this operation when you're viewing a post on the front-end.

It appears you're using the incorrect methods. WordPress is kind of tricky sometimes, and in this case you want to be using get_previous_post_link instead of get_previous_posts_link (note: the difference is that in the method you are calling, posts is plural - in the method you want, post is singular).

So give these a shot

EDIT: Here is an updated example based on your code

add_filter( 'the_content', 'post_navigation', 10, 2 );

function post_navigation($content) {
    $post = get_post();

    if ( 'post' === $post->post_type ) {
        $next_p  = get_next_post_link();
        $prev_p  = get_previous_post_link();
        $content = "{$content}<br/>{$prev_p} | {$next_p}";
    }

    return $content;    
}
  • Thank you very much! Marvelous, but the question is: Why I didn't find it in the Codex? Why just the get_next_posts one? – Geeocode Feb 23 '18 at 16:57
  • @Geeocode I can't really say why it's not in the Codex. It's been around since WP 3.7.0, so I feel it must be due to some oversight from the WP documentation team. Regardless, it does have entries on the Developer Handbook (linked in my answer), and that documentation will continue to be updated more frequently than the codex in the future. Also, please consider Mark Kaplun's advice that this should not be done via the_content – phatskat Feb 23 '18 at 17:00
  • Could you please determine where could I put this decoration to avoid any unwanted interaction? Thank you in advance! – – Geeocode Feb 23 '18 at 21:18
  • I start another question with it. – Geeocode Feb 23 '18 at 21:29
3

Just don't do it. The the_content filter supposed to automatically amend the content, while what you are trying to do is to add a decoration, not a content. You are going to hurt in very unexpected ways depending on where exactly you set the filter and how well you remove it after your intended use. (for example, place it in a global scope and your excerpts might get the links)

If you have to manipulate the content being displayed by adding a decoration to it, do it in the theme, no other place.

  • Ah great point - I was so focused on making it work that I didn't even take this into account. Filtering the_content can indeed lead to unexpected results in odd places. The much better approach is to modify a theme template to display these links instead. – phatskat Feb 23 '18 at 16:57
  • Thank you for your answer too, I gave an up for it. I'm quite new at Wordpress, so when I was searching for a place where I can place a hook for decoration I read an article of a developer who said that the most common place to place a hook is the_content. So that was the very first hook I knew. :) – Geeocode Feb 23 '18 at 17:09
  • Could you please determine what does it mean exactly "do it in the theme"? Thank you in advance! – Geeocode Feb 23 '18 at 21:07
  • I start another question with it. – Geeocode Feb 23 '18 at 21:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.