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
-
can anyone help me pleeeease? 🙂
Middle solution here: http://wordpress.org/support/topic/284156?replies=2
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">« Go Forward in Time</a> ∞ <a href="<?php next_posts(); ?>#lemonlines">Go Back in Time »</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">« 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 »</a> // only link to older posts <?php } else { ?> //otherwise <a href="<?php previous_posts(); ?>#anchor">« Go Forward in Time</a> ∞ <a href="<?php next_posts(); ?>#anchor">Go Back in Time »</a> // links to newer and older posts <?php } ?>
Cheers,
geloTo 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 »</a><?php } ?> </div>
- The topic ‘Anchor link with posts_nav_link’ is closed to new replies.