WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] add class to anchor in next_post_link & previous_post_link (5 posts)

  1. matt_g
    Member
    Posted 2 years ago #

    Hi,

    I've seen this asked a number of times, but haven't seen a solution that was straightforward enough for me to use.

    I need to add a class to the anchor tag that's returned by next_post_link and previous_post_link

    While I appreciate people have suggested things like wrapping the php tag in another pair of tags like a span and classing that, or the fact that next_post_link allows you to place tags around the anchor, that is not a solution for this case.

    What I am trying to do, can only be achieved with a class on the anchor. I've tried numerous other combinations of nesting classes on parent objects, and it doesn't work.

    I've achieved a similar outcome for the next_posts_link and previous_posts_link to navigate between category pagination by adding this to the functions.php file

    add_filter('next_posts_link_attributes', 'posts_link_attributes');
    add_filter('previous_posts_link_attributes', 'posts_link_attributes');
    
    function posts_link_attributes(){
       return 'class="internav"';
    
    }

    however I don't know enough to know how that works, except that the return bit seems to add whatever you put in there to the output that next_posts_link spits out into the HTML

    Could someone please give me a solution for dummies that i can just copy & paste into my functions.php, so that I can try to learn from seeing a working example?

    Thanks.

  2. vtxyzzy
    Member
    Posted 2 years ago #

    The problem is that previous/next_post_link() does not have a filter, so you can't add something to functions.php to alter the output.

    Also, there is no option to return the link rather than echoing it, so there is no straightforward way to assign the output to a variable.

    The only way that I know to do this is to replace the call to previous/next_post_link with code to capture the output by buffering and then alter it before echoing it.

    In other words, you would need to edit the template file and replace this, or similar depending on your theme:

    previous_post_link( '%link', '<span class="meta-nav">' . _x( '&larr;', 'Previous post link', 'twentyten' ) . '</span> %title',true );

    with this:

    ob_start();
    previous_post_link( '%link', '<span class="meta-nav">' . _x( '&larr;', 'Previous post link', 'twentyten' ) . '</span> %title',false );
    $link = ob_get_clean();
    echo str_replace('<a ','<a class="myclass" ', $link);
  3. matt_g
    Member
    Posted 2 years ago #

    Thanks for the response. Strangely enough, after pulling my hair out on dozens of combinations, and a night's sleep, it turned out that I was able to find a very specific nesting order in the CSS that Opera would render - which was the reason for the whole problem in the first place.

    Thanks for posting a solution - hopefully it'll help other people. One question though - all the references to twentyten - does that require you have the functions file from twentyten in your theme? I generally like to build themes from scratch without anyone else's functions included, just so I can know every deviation from core is one I put in manually.

    Cheers.

  4. Kat Hagan
    Member
    Posted 2 years ago #

    For anyone else curious, the reference to the twentyten theme is a translation/localization function. If you are making your own theme, you would change "twentyten" to be your theme's name -- just tells it where to look for translation files.

    http://phpdoc.wordpress.org/trunk/WordPress/i18n/_wp-includes---l10n.php.html#function_x

  5. egr102
    Member
    Posted 2 years ago #

    Be very careful with @vtxyzzy's code. I've tried it and my hosting provider shut my site down because basically @vtxyzzy's code was seen as a malware/phishing attack, i.e. the code produces a PHP injection.

Topic Closed

This topic has been closed to new replies.

About this Topic