Home › Forums › Support › in the post navigation, how to add links to the first and most recent post?
- This topic has 9 replies, 3 voices, and was last updated 4 years, 4 months ago by
Tom.
-
AuthorPosts
-
December 2, 2018 at 10:21 pm #746435
Danpob
Hello,
I want to have a post navigation bar similar to how webcomics are formatted:
There’s a link where you can navigate to the first post, previous post, next post, and last post in a category.So far, I’ve only managed to get the ‘previous’ and ‘next’ post, but I’d like to have a link to the First and Last post as well.
Is there any easy way to achieve this?
Thanks!
December 3, 2018 at 8:40 am #746895Tom
Lead DeveloperLead DeveloperHi there,
Any chance you can link us to a page where we can see an example?
Let us know π
December 3, 2018 at 6:25 pm #747300Danpob
Hi Tom,
Here’s an example from a webcomic (http://trenchescomic.com/comic/post/19191)
Here’s an example from a comic that uses the WP’s ComicEasel plugin and Comic Press theme (http://www.happyjar.com/)
(The ComicEasel plugin doesn’t really fit in with what I want to do, so I prefer to customize GeneratePress instead)
I need the post navigation to go to the ‘first’, ‘previous’, ‘next’, and ‘last/most recent’ post. Currently, what’s built in to GeneratePress seems to be only the ‘previous’ and ‘next’ post navigation.
Thanks!
December 4, 2018 at 10:23 am #747992Tom
Lead DeveloperLead DeveloperYou could do something like this:
add_filter( 'previous_post_link', function( $output ) { $latest_post = get_posts( 'numberposts=1' ); $latest_post = sprintf( '<div class="latest-post"><a href="%1$s" title="%2$s">First Post</a></div>', get_permalink( $latest_post[0]->ID ), get_the_title( $latest_post[0]->ID ) ); echo $latest_post . $output; } ); add_filter( 'next_post_link', function( $output ) { $last_post = get_posts( 'numberposts=1&order=ASC' ); $last_post = sprintf( '<div class="last-post"><a href="%1$s" title="%2$s">Last Post</a></div>', get_permalink( $last_post[0]->ID ), get_the_title( $last_post[0]->ID ) ); echo $output . $last_post; } );
Of course, you’d need to style it to look however you want π
December 5, 2018 at 6:44 am #748657Danpob
Hi Tom,
Thanks but this seems like it’s replacing “previous” and “next” with ‘first’ and ‘last’ post?
I would like to have all four: first, previous, next, and last ?December 5, 2018 at 9:34 am #748902Tom
Lead DeveloperLead DeveloperIn my tests, it doesn’t replace the original links.
However, it does add “First” and “Last” while the other links use their post title.
If you want First, Previous, Next and Last, you’d want to do this:
add_filter( 'next_post_link', function( $output, $format, $link, $post ) { if ( ! $post ) { return ''; } $next = sprintf( '<div class="nav-next"><span class="next"><a href="%1$s" title="%2$s">Next</a></span></div>', get_permalink( $post ), $post->post_title ); $last_post = get_posts( 'numberposts=1&order=ASC' ); $last_post = sprintf( '<div class="last-post"><a href="%1$s" title="%2$s">Last</a></div>', get_permalink( $last_post[0]->ID ), get_the_title( $last_post[0]->ID ) ); return $next . $last_post; }, 10, 4 ); add_filter( 'previous_post_link', function( $output, $format, $link, $post ) { if ( ! $post ) { return ''; } $prev = sprintf( '<div class="nav-previous"><span class="prev"><a href="%1$s" title="%2$s">Previous</a></span></div>', get_permalink( $post ), $post->post_title ); $latest_post = get_posts( 'numberposts=1' ); $latest_post = sprintf( '<div class="latest-post"><a href="%1$s" title="%2$s">First</a></div>', get_permalink( $latest_post[0]->ID ), get_the_title( $latest_post[0]->ID ) ); return $latest_post . $prev; }, 10, 4 );
January 25, 2019 at 5:40 pm #792490Todd
Beginner question on this, but hopefully an easy one to answer: Where would I stick the php you posted above to get the nav working (assuming I have a child theme)?
January 25, 2019 at 5:48 pm #792492Tom
Lead DeveloperLead DeveloperIf you have a child theme, it would go at the bottom of your
functions.php
file (in the child theme).More info on that method (and others) here: https://docs.generatepress.com/article/adding-php/
January 25, 2019 at 6:39 pm #792511Todd
The following is the entirety of what’s in my functions.php and it doesn’t change the default paging nav. If you want to take a look, I think you have web address from the thread where I was harassing you about the white space under the post image. π Login: tom/generate
<?php add_filter( 'next_post_link', function( $output, $format, $link, $post ) { if ( ! $post ) { return ''; } $next = sprintf( '<div class="nav-next"><span class="next"><a href="%1$s" title="%2$s">Next</a></span></div>', get_permalink( $post ), $post->post_title ); $last_post = get_posts( 'numberposts=1&order=ASC' ); $last_post = sprintf( '<div class="last-post"><a href="%1$s" title="%2$s">Last</a></div>', get_permalink( $last_post[0]->ID ), get_the_title( $last_post[0]->ID ) ); return $next . $last_post; }, 10, 4 ); add_filter( 'previous_post_link', function( $output, $format, $link, $post ) { if ( ! $post ) { return ''; } $prev = sprintf( '<div class="nav-previous"><span class="prev"><a href="%1$s" title="%2$s">Previous</a></span></div>', get_permalink( $post ), $post->post_title ); $latest_post = get_posts( 'numberposts=1' ); $latest_post = sprintf( '<div class="latest-post"><a href="%1$s" title="%2$s">First</a></div>', get_permalink( $latest_post[0]->ID ), get_the_title( $latest_post[0]->ID ) ); return $latest_post . $prev; }, 10, 4 );
January 26, 2019 at 9:12 am #792953Tom
Lead DeveloperLead DeveloperAny chance you can open a fresh topic so the original author of this one doesn’t get a bunch of notifications?
Let me know π
-
AuthorPosts
- You must be logged in to reply to this topic.