WordPress.org

Support

Support » Themes and Templates » [Resolved] Anchor link with posts_nav_link

[Resolved] Anchor link with posts_nav_link

  • 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

Viewing 5 replies - 1 through 5 (of 5 total)
  • can anyone help me pleeeease? 🙂

    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?

    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

    lassemahn
    Member

    @lassemahn

    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>
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘[Resolved] Anchor link with posts_nav_link’ is closed to new replies.