WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Anchor link with posts_nav_link (6 posts)

  1. jpajares
    Member
    Posted 5 years ago #

    I'm designing a new theme with a big header so it needs anchor links to jump directly to the posts in several situations. I hadn't had problems with this bynow but now I'm not able to add an anchor link in next and previous posts links.

    How can i add an anchor link to the posts_nav_link function?

    Cheers,
    gelo

  2. jpajares
    Member
    Posted 5 years ago #

    can anyone help me pleeeease? :)

  3. jpajares
    Member
    Posted 5 years ago #

  4. jpajares
    Member
    Posted 5 years ago #

    The previous solution was an ugly hack that now I've discarded.

    Someone can tell me how to add an anchor link to the output of posts_nav_link?

  5. jpajares
    Member
    Posted 5 years ago #

    Finally, I've found the solution HERE!!

    There are two undocumented functions that will return the Next Posts and Previous Posts URLs:next_posts () & previous_posts ()

    The syntax for creating a navigation with anchor links would be something like:

    <a href="<?php previous_posts(); ?>#lemonlines">&laquo; Go Forward in Time</a> ∞ <a href="<?php next_posts(); ?>#lemonlines">Go Back in Time &raquo;</a>

    The problem here is that both links will still show even when we are in home or in the last page. To show only appropriate links use:

    <?php
    	$the_last_page = $wp_query->max_num_pages;
    	$loaded_page = intval($paged);
    ?>
    <?php if ( $the_last_page == $loaded_page) { ?> // if is the last page
    	<a href="<?php previous_posts(); ?>#anchor">&laquo; Go Forward in Time</a> //  only link to newer posts
    <?php } elseif ($loaded_page == 0) { ?> // if is home
    	<a href="<?php next_posts(); ?>#anchor">Go Back in Time &raquo;</a> // only link to older posts
    <?php } else { ?> //otherwise
    	<a href="<?php previous_posts(); ?>#anchor">&laquo; Go Forward in Time</a> ∞ <a href="<?php next_posts(); ?>#anchor">Go Back in Time &raquo;</a> // links to newer and older posts
    <?php } ?>

    Cheers,
    gelo

  6. lassemahn
    Member
    Posted 4 years ago #

    To solve the problem of only showing the "Go forward in time"-link when there are posts, do this instead for a bit cleaner code:

    <div class="alignright">
    <?php $are_there_posts = previous_posts_link('next'); if ($are_there_posts != "") {  ?>
    				<a href="<?php previous_posts();?>#abstopp">Go forward in time &raquo;</a><?php } ?>
    </div>

Topic Closed

This topic has been closed to new replies.

About this Topic