• Pretty basic question…

    next_post_link() displays a link to the next post. It’s first paramter allows you to wrap the link in markup, like this:

    next_post_link('<strong>%link</strong>');

    But how do I actually add a class to the link/anchor itself?

    Alternatively, I can’t seem to find a function that returns the next or previous post/page ID. Is there anything like that out there?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter jakobud

    (@jakobud)

    Wrap the call to next_post_link() in a div or other block, like this:

    <div class='mynextlink'><?php next_post_link(...);?></div>

    Same question really…

    I need to be a little more specific than vtxyzzy‘s solution as I need to give the actual <a> element its own class and also find away to retrieve the Post ID (pid) of previous and next posts.

    I’m trying a few things with my limited knowledge and the help of the codex but haven’t got anywhere close yet.

    Any help would be great!

    jakobud, did you ever get anywhere with this?

    Many thanks
    Stef

    you can get the ids in the loop:

    <?php $next_post_id = get_next_post()->ID; ?>
    <?php $prev_post_id = get_previous_post()->ID; ?>

    https://codex.www.ads-software.com/Function_Reference/get_next_post
    https://codex.www.ads-software.com/Function_Reference/get_previous_post

    I need to give the actual element its own class

    do you need the link class to be dynamically generated using the id of the next post?
    or is it not enough to adress the style of your link as in @vtxyzzy example .mynextlink a { .... }

    or build your own link, for instance with the url of the next post:
    get_permalink($next_post_id)

    https://codex.www.ads-software.com/Function_Reference/get_permalink

    Thanks alchymyth.

    For various reasons-

    (Posts opening within a lightbox (hence the need for the class on the <a>), Using a secondary simplified template (for the lightbox) and also having nav between posts within the lightbox itself required the posts to be called and navigated using post ID instead of permalinks as the permalink URLs point to the post on my themes full single.php template (which I also need)

    – I needed to call the previous and next posts by ID and dynamically build my own links with specific classes added directly to the <a> so that my nav and lightbox would work.

    I’ve taken hints from the posts above in this thread, but in the end figured it for my self using get_adjacent_post() to get post IDs and making my own links – probably a very inefficient hack but it works for me.

    Here’s my code – I’m a beginner so it’s fairly rough and I’m sure many of you could find a far better way to do this but it works for me. Now I’ve re-read the posts above I’m fairly positive that I’ve taken a slightly long an abstract approach.

    I’ve included a quick method for checking that we’re at the first or last post so I can remove the nav when necessary.

    <div class="lightbox-product-nav">
    
    <span id="prev">
    <?php if(!get_adjacent_post(false, '', false)) { echo '';
    } else
    { $adjacent_post = get_adjacent_post(true,'3,16,17',false) ; ?> <strong>&laquo;</strong><a class="fancybox" href="lightbox/?pid=<?php echo $adjacent_post->ID; ?>"> <strong>Previous product:</strong> <?php echo $adjacent_post->post_title;?></a><?php } ?>
    </span>
    
    <span id="next">
    <?php if(!get_adjacent_post(true, '', true)) { echo '';
    } else
    { $adjacent_post = get_adjacent_post(true,'3,16,17',true) ;?>
    <a class="fancybox" href="lightbox/?pid=<?php echo $adjacent_post->ID; ?>"><strong>Next product:</strong>  <?php echo $adjacent_post->post_title; ?></a>  <strong>&raquo;</strong><?php } ?>
    </span>
    
    </div>
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How to add class to next_post_link anchor?’ is closed to new replies.