Single post navigation links are valuable because they link all your posts together. This increases page views and acts a bit like a sitemap which the search engines can follow.
Its pretty easy to add next and previous nav links at the end of all your single posts. But what about adding them to all posts using one specific post type?
As an example, you may have a portfolio where you want all single posts assigned to that specific portfolio post type linked together. And you may not want to link other post types to the posts assigned to this custom post type.
Simply copy the code below from the view raw link & paste it in your child themes functions.php file using a text editor.
Here’s 2 different code snippets which enable you to link all posts assigned to any custom post type.
add_action( 'genesis_entry_footer', 'wpsites_single_cpt_navigation' );
function wpsites_single_cpt_navigation() {
if ( is_singular( 'portfolio' ) ) {
echo'<div class="alignleft">';
previous_post_link();
echo'</div>';
echo'<div class="alignright">';
next_post_link();
echo'</div>';
} }
You can also code it like this which checks if your child theme is using XHTML or HTML 5 markup and hooks:
add_action( 'genesis_entry_footer', 'wpsites_single_cpt_navigation' );
function wpsites_single_cpt_navigation() {
if ( ! is_singular( 'portfolio' ) )
return;
genesis_markup( array(
'html5' => '<div %s>',
'xhtml' => '<div class="navigation">',
'context' => 'adjacent-entry-pagination',
) );
echo '<div class="pagination-previous alignleft">';
previous_post_link();
echo '</div>';
echo '<div class="pagination-next alignright">';
next_post_link();
echo '</div>';
echo '</div>';
}
Hi Brad,
is there a way to set next and previous links for CPT to order alphabetically instead of chronologically?
The code for the pre-HTML 5 version didn’t work for me on the Minimum 2.x theme. I submitted it to Genesis support and a wonderfully helpful lady named Amelia debugged it for us. All you need to do is remove the “TRUE” argument in each of the two functions. I did this and it worked perfectly.
Great. Thanks for the feedback Will.
The last snippet on this page does include the 3rd parameter set to true and it works.
I did remove it entirely from the 1st 2 snippets and now they work so much appreciated as i did have this post saved and have been trying to work it out for some time.
I’ll update the Codex because that’s not mentioned for the in_same_term parameter.
thanks so much for this!
is it possible to have the title of the next post appear, instead of the word “next” or “previous”? if so, how would i go about that?
Yes replace the text with %title for both previous and next post links
i thought i was going to get this with some trial and error but i think i’m missing something with the placement of the %title. i tried replacing the words ‘next’ and ‘previous’, and i also replaced everything in the braces (amongst some other tries).
what i’m getting is some garbled text instead of simply the title.
what did you mean by ‘replace the text’?
and thanks again for your suggestions.
emily
https://gist.github.com/cf174707748f0aa5f429.git
Is this for a custom post type or single posts?
Try this http://wpsites.net/web-design/different-ways-to-add-post-navigation-pagination-in-genesis/
The parameters are different for different functions.
The old functions use title whereas the new ones use %title
oh gosh, it looks like neither of my links came over in that last post, sorry. i’m working with single posts within a custom post type.
my placements of %title on that page:
one more try on this gist (im unclear on how to make them look good like yours):
https://gist.github.com/anonymous/7cf07aa620d800d09588#file-gistfile1-txt
Hi Brad,
Did you work out this code to include classes?
What I’d like to do is have “Previous” float:left and “Next” float:right in my portfolio page, ideally using images. I’m using the Minimum Pro theme.
Thanks for this tutorial.
Hi Danica
Post updated: Its simply a matter of wrapping each link in a class.
I’m trying to use your code to apply previous/next to my “vignette” CPT, but it brings my site down:
Do you see anything obviously wrong?
Lang
Oh my; I see that the middle section of the code (the part with all the “php”s) was mostly removed from my message. Is there an easy way to avoid that from happening?
You can paste the code into a Gist and embed it or wrap the code in code shortcodes.
I’m trying to use your code to add previous/next links to my “vignette” CPT. But it brings my site down. See anything wrong?
Got it to work! There was something wrong with my label at the beginning, where you are mentioned. The following works just fine; all that’s left is styling the links to my liking:
I see that the code still isn’t displayed properly. Oh well, I got it work! And thanks muchly for the code!
Hello Lang
The code doesn’t include a class so therefore cannot be styled.
Its something i need to update or find another solution which includes a class. Now Updated.
Brad:
I just created a github account and this is my first try at posting a gist.
The first instance of code shows how I got very nice previous-next buttons at the end of my blog posts, which actually uses the names of previous and next posts as the links. The second piece of code is what I’ve just used to enable previous-next links for my “vignette” cot. However, these links only say “previous” and “next” … they are not the actual post names. Is there an easy way to get previous-ext for my “vignette” cpt to display just like in my blog posts, with the names of the previous and next posts displayed as links?
Here’s an example of a Gist
You would need to modify the code which is something i need to work out so it uses the same classes for styling as the theme.
Brad:
I’ve added your code to my vignette CPT and it works great, but the links appear just below my Jetpack Sharing buttons. Is there an easy way to have the previous-next links appear above the sharing buttons? See example here:
http://miracleofnature.org/vignette/sample-vignette-wide-1014px
Lang
You can either:
1. Change the hook position for either the sharing buttons or pagination.
2. Add a 3rd parameter for priority positioning to either the sharing buttons function or the function for pagination
Don’t you ever sleep? If you’re in Victoria, it’s now sunday morning at 5am.
I actually tried prioritizing your code to “1”, but that didn’t move it up. So the Sharing buttons must somehow be holding on tight to the upper spot.
Yes, I can move the sharing buttons down to before_entry_comments, but that puts the buttons below the authorbox, which is no good (the buttons need to be above the authorbox).
Hmmm?
Yes because they use a WordPress hook for the content so there’s no way to output above them unless you reposition the buttons with the next hook.
I do need to write a better snippet for this as its not very flexible.
OK, so I’ll move the authorbox down to “genesis_entry_footer” and give it a low number priority. Then I’ll try moving the share buttons down to the same hook, giving them a higher number priority.
Will try this later this afternoon.
Wow! I wish I found this a month ago when I was building a site that needed this. I spent too many hours between figuring this and adding a custom sidebar on CPTs that displayed custom fields; I ended up just using get_template_part().
Yeah me too Zach.
Really needed this last year for a series of posts but couldn’t find it.
You are welcome to republish this code as long as you link back to my site!! haha
P.S. Like your post on Conditional tags.
Where’s the “LOVE” button on this! I have seen a lot of people asking for this, most recently on the SP forum. I will make sure I share this one. Thanks Brad!
Yeah its a really cute little solution. Hope its popular.
Took me a while to work this out because the conditional tag isn’t really one after all.
Wanted to add this myself for a long time so i know it will be useful on the forums.
Anytime Anita.