• Resolved Stian Lund

    (@pathduck)


    Hi!
    I have a simple child theme where I’ve overridden a method to format posts/pages navigation links.

    It contains the following code for the single posts:

    
    <?php if ( is_single() ) : // navigation links for single posts ?>
    
    	<?php next_post_link( '<div class="nav-previous">%link</div>', '<span class="meta-nav">' . _x( '&larr;', 'Previous post link', 'wp386' ) . '</span> %title' ); ?>
    	<?php previous_post_link( '<div class="nav-next">%link</div>', '%title <span class="meta-nav">' . _x( '&rarr;', 'Next post link', 'wp386' ) . '</span>' ); ?>
    

    The problem here is that the previous_post_link() inserts a value of rel="prev" (obviously) – while I would like it to insert a rel="next" instead, even if that seems counter-intuitive.

    Is there a simple way to change this using filters or some basic logic? I’m not very skilled with PHP but know my way around modifying existing code if needed. I’ve tried reading some of the docs but didn’t get any wiser unfortunately.

    For instance, I managed to add rel links to the page/category pagination links on the main pages using the following relatively simple filter:

    
    // Add rel attributes to next/previous links
    add_filter('next_posts_link_attributes', function(){return 'rel="next"';});
    add_filter('previous_posts_link_attributes', function(){return 'rel="prev"';});
    

    But for single posts I’m struggling to find a *simple* solution without having to write my own function(s) or overriding system functions. Apparently there is no filter for next_post_link_attributes?

    One of the reasons I’d like *previous* posts to have the rel="next" attribute is for working with the Vivaldi browser’s “Fast Forward” feature which looks for these kinds of attributes.

    • This topic was modified 2 years ago by Stian Lund.
Viewing 6 replies - 1 through 6 (of 6 total)
  • Hi @pathduck

    You’re going to need to add some code to your theme’s functions.php file. What theme are you using? I’ve tested the following code with Twenty Twenty-One theme:

    
    add_filter( 'previous_post_link', 'change_prev_attributes' );
    add_filter( 'next_post_link', 'change_next_attributes' );
    function change_prev_attributes( $format )
    {
    
        $format = str_replace(
            'rel="prev"',
            'rel="next"',
            $format
        );
    
        return $format;
    }
    function change_next_attributes( $format )
    {
    
        $format = str_replace(
            'rel="next"',
            'rel="prev"',
            $format
        );
    
        return $format;
    }
    Little Package

    (@littlepackage)

    Try something like this:

    function my_custom_next_post_link( $output, $format, $link ) {
        $output = str_replace( 'next', 'prev', $output );
        return $output;
    } 
    add_filter( 'next_post_link', 'my_custom_next_post_link', 10, 3 );

    Reverse the code for a previous link:

    function my_custom_prev_post_link( $output, $format, $link ) {
        $output = str_replace( 'prev', 'next', $output );
        return $output;
    } 
    add_filter( 'previous_post_link', 'my_custom_prev_post_link', 10, 3 );

    Seems like using filters is your best bet. I suppose you could also use JavaScript.

    Thread Starter Stian Lund

    (@pathduck)

    @mburridge @littlepackage
    Thank you SO much for the tips! ??

    I could swear I tried using add_filter for the next/previous_post_link but couldn’t get it to work, or I didn’t understand that I needed to use str_replace() with an input argument.

    I went with the anon function like I did with the other posts type. It’s a bit of a “mess” but saves me adding another function inside a function (this is all in a separate require call from functions.php). But might be bad for performance reasons, what do you think?

    
    // Swap 'rel' attributes for single post next/previous links 
    add_filter( 'previous_post_link', function($format){return str_replace('rel="prev"','rel="next"',$format);});
    add_filter( 'next_post_link', function($format){return str_replace('rel="next"','rel="prev"',$format);});
    

    Thanks again helpful people! ??

    EDIT: Oh and the theme I use is this:
    https://github.com/keichan34/wp386
    It’s getting OLD, not updated by the author for a decade… And will probably stop working at some point. Making small changes to it is just a way to *slowly* learn a little more PHP.

    I’m adding the code in the overridden wp386_content_nav() method here:
    https://github.com/keichan34/wp386/blob/master/inc/template-tags.php

    • This reply was modified 2 years ago by Stian Lund.
    • This reply was modified 2 years ago by Stian Lund.
    • This reply was modified 2 years ago by Stian Lund.
    • This reply was modified 2 years ago by Stian Lund.
    Little Package

    (@littlepackage)

    @pathduck

    Technically, $format is the second argument of these filters, not the first, so you are filtering $output as I suggested, not the $format.

    // Swap 'rel' attributes for single post next/previous links 
    add_filter( 'previous_post_link', function($output, $format, $link){return str_replace('rel="prev"','rel="next"',$output);});
    add_filter( 'next_post_link', function($output, $format, $link){return str_replace('rel="next"','rel="prev"',$output);});

    Jus’ sayin’… Also, I don’t know what you mean by function in a function? Finally, anonymous functions aren’t only more difficult to read but they’re not going to parse any faster than the usual way of doing things.

    Thread Starter Stian Lund

    (@pathduck)

    @littlepackage Thanks for the clarification ??

    Also, I don’t know what you mean by function in a function?

    The code for displaying nav links in the parent theme lives in wp386_content_nav() function in inc\template-tags.php.

    I’ve overridden this single function in my child theme in the same place. So it’s more logical/cleaner to add any filters inside there. So I could of course add functions inside *that* function – or in functions.php, but I think it looks a *little* cleaner to do it this way.

    Finally, anonymous functions aren’t only more difficult to read but they’re not going to parse any faster than the usual way of doing things.

    I was thinking more the other way around, if anonymous functions would be a performance drawback. But no big deal, my site is just for my own fun – no traffic *at all* ??

    • This reply was modified 2 years ago by Stian Lund.
    • This reply was modified 2 years ago by Stian Lund.

    There some discussion of this issue here: https://core.trac.www.ads-software.com/ticket/55751

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Swapping next/prev link attributes for single post navigation?’ is closed to new replies.