WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Disable "previous link" in last post and "next link" in first post (3 posts)

  1. melvyn
    Member
    Posted 5 years ago #

    Hi all!

    I'm using this for my next/previous link instead of the standard next_post_link/previous_post_link because I needed to style the link:

    <div id="post-nav-next"><a href="<?php $nextPost = get_next_post(true); echo get_permalink($nextPost->ID); ?>#post-content"></a></div>
    <div id="post-nav-previous"><a href="<?php $prevPost = get_previous_post(true); echo get_permalink($prevPost->ID); ?>#post-content"></a></div>

    The problem with this is that the previous link still shows up in the last post and likewise the next link is there in the first post. How do I disable or hide this? I presume it involves the "if" php function but I'm still no good with php yet.

    Any help would be appreciated. Thanks!

  2. melvyn
    Member
    Posted 5 years ago #

    Actually, I used this rather than the next_post_link/previous_post_link so that I can add the jump anchor "#target" to the link.

    Anyone?

  3. melvyn
    Member
    Posted 5 years ago #

    Found a solution from a previous topic and modified it to this:

    <div id="post-nav-previous">
    <?php $next_post = get_next_post(true); if($next_post) { echo "\t" . '<a rel="next" href="' . get_permalink($next_post->ID) . '#post-content"></a>' . "\n"; } ?>
    </div>
    
    <div id="post-nav-next">
    <?php $prev_post = get_previous_post(true); if($prev_post) { echo "\t" . '<a rel="next" href="' . get_permalink($prev_post->ID) . '#post-content"></a>' . "\n"; } ?>
    </div>

    Worked like a charm!

Topic Closed

This topic has been closed to new replies.

About this Topic