Add Custom Post Type Navigation Links in Genesis Themes

You can add post navigation links to custom post types in Genesis WordPress themes by using an existing function and just extending it. That function is genesis_prev_next_post_nav and is found in genesis/lib/structure/post.php

The function just refers to posts in the code but you can extend that by adding your own custom post types to an array which then add the navigation links to the posts that belong to the same custom post type.

So in your functions.php you can add a code snippet

<?php //<~ dont add me in
add_action( 'genesis_entry_footer', 'wpb_prev_next_post_nav_cpt' );
function wpb_prev_next_post_nav_cpt() {
if ( ! is_singular( array( 'portfolio', 'post' ) ) ) //add your CPT name to the array
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>';
}
view raw cpt-nav.php hosted with ❤ by GitHub

Just add your own custom post type name, if you have just one custom post type that you want to have navigation links for, say only portfolio and not regular posts, you can just drop the array and add in

<?php //<~ dont add me in
add_action( 'genesis_entry_footer', 'wpb_prev_next_post_nav_cpt' );
function wpb_prev_next_post_nav_cpt() {
if ( ! is_singular( 'portfolio' ) ) //add your CPT name
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>';
}
view raw cpt-nav-single.php hosted with ❤ by GitHub

The navigation links will appear in the footer of the post as we have added the function to be hooked into genesis_entry_footer

Navigation Previous/Next Text

By default the titles of the post are used as the linked text but you can change these – look at the code below and at the previous_post_link and next_post_link functions.

<?php //<~ dont add me in
add_action( 'genesis_entry_footer', 'wpb_prev_next_post_nav_cpt' );
function wpb_prev_next_post_nav_cpt() {
if ( ! is_singular( array( 'portfolio', 'post' ) ) ) //add your CPT name to the array
return;
genesis_markup( array(
'html5' => '<div %s>',
'xhtml' => '<div class="navigation">',
'context' => 'adjacent-entry-pagination',
) );
echo '<div class="pagination-previous alignleft">';
previous_post_link('&laquo; %link', 'Previous Post'); // Change nav text here
echo '</div>';
echo '<div class="pagination-next alignright">';
next_post_link(' %link &raquo;', 'Next Post'); // Change nav text here
echo '</div>';
echo '</div>';
}
view raw labechange.php hosted with ❤ by GitHub

 

Ref

4 Comments

  1. Noah on September 26, 2016 at 8:46 am

    Awesome just what I needed, thanks!

  2. Marj Esch on July 22, 2016 at 12:40 pm

    Thank you so much! This is exactly what I was looking for. It’s perfect.

  3. Shon on March 19, 2016 at 8:20 am

    Thanks so much! This is exactly what I was looking for.

    One question though:

    How do I display the words “Next” and “Previous” instead of the post link?

    I’m guessing it has to do with replacing these functions at the end:

    previous_post_link();

    next_post_link();

    Your help is greatly appreciated!

Leave a Comment





%d bloggers like this: