How to Customize Post Navigation Links in Twenty Fifteen
Content
Many users of the Twenty Fifteen theme want to modify the default post navigation links that appear at the bottom of single posts. Common requests include limiting navigation to posts within the same category, changing the styling based on category, or completely removing these links to implement a custom solution. This guide explains how to achieve these customizations.
Understanding the Default Navigation
The post navigation in Twenty Fifteen is generated by the the_post_navigation() function, which is located in the theme's single.php template file. By default, this function creates "Previous" and "Next" links that navigate through all posts chronologically, regardless of their category.
Common Customization Requests and Solutions
1. Limit Navigation to the Same Category
To make the "Previous" and "Next" links navigate only within the current post's category, you need to modify the function's arguments. This requires creating a child theme and editing its single.php file.
Locate the following code in your child theme's single.php:
// Previous/next post navigation.
the_post_navigation( array(
'next_text' => '<span class="meta-nav" aria-hidden="true">' . __( 'Next', 'twentyfifteen' ) . '</span> ' .
'<span class="screen-reader-text">' . __( 'Next post:', 'twentyfifteen' ) . '</span> ' .
'<span class="post-title">%title</span>',
'prev_text' => '<span class="meta-nav" aria-hidden="true">' . __( 'Previous', 'twentyfifteen') . '</span> ' .
'<span class="screen-reader-text">' . __( 'Previous post:', 'twentyfifteen' ) . '</span> ' .
'<span class="post-title">%title</span>',
) );
Replace it with code that uses the in_same_term parameter:
// Previous/next post navigation within the same category.
$previous = get_previous_post( TRUE );
$next = get_next_post( TRUE );
?>
<nav class="navigation post-navigation" role="navigation">
<h2 class="screen-reader-text"><?php _e( 'Post navigation', 'twentyfifteen' ); ?></h2>
<div class="nav-links">
<?php if ( ! empty( $previous ) ) : ?>
<div class="nav-previous">
<a href="<?php echo get_permalink( $previous ); ?>" rel="prev">
<span class="meta-nav" aria-hidden="true"><?php _e( 'Previous', 'twentyfifteen' ); ?></span>
<span class="screen-reader-text"><?php _e( 'Previous post:', 'twentyfifteen' ); ?></span>
<span class="post-title"><?php echo get_the_title( $previous ); ?></span>
</a>
</div>
<?php endif; ?>
<?php if ( ! empty( $next ) ) : ?>
<div class="nav-next">
<a href="<?php echo get_permalink( $next ); ?>" rel="next">
<span class="meta-nav" aria-hidden="true"><?php _e( 'Next', 'twentyfifteen' ); ?></span>
<span class="screen-reader-text"><?php _e( 'Next post:', 'twentyfifteen' ); ?></span>
<span class="post-title"><?php echo get_the_title( $next ); ?></span>
</a>
</div>
<?php endif; ?>
</div>
</nav>
<?php
The key change is the TRUE parameter in get_previous_post( TRUE ) and get_next_post( TRUE ), which tells WordPress to only find posts within the same category.
2. Remove the Navigation Completely
If you prefer to remove the default navigation entirely to add your own custom links, you can simply delete or comment out the entire the_post_navigation() function call in your child theme's single.php file. Look for the section containing this function and remove it.
3. Style Links Based on Category
To style the navigation links with different colors for each category, you can use CSS. The navigation links likely already have CSS classes for the post category. You can inspect these classes using your browser's developer tools and then target them in your custom CSS.
For example, you might add CSS like this in Appearance > Customize > Additional CSS:
.nav-links a.category-news { background-color: blue; }
.nav-links a.category-blog { background-color: green; }
Important Note on Child Themes
Always use a child theme when making modifications to theme files. This ensures your changes are not overwritten when the Twenty Fifteen theme receives updates. Directly editing the parent theme's files is strongly discouraged.
By understanding how the post navigation function works in single.php, you can effectively customize it to better suit your website's navigation needs.
Related Support Threads Support
-
display category sub category name as links in posthttps://wordpress.org/support/topic/display-category-sub-category-name-as-links-in-post/
-
Show Icon or Image Before Titlehttps://wordpress.org/support/topic/show-icon-or-image-before-title/
-
Remove Previous & Next Post Backgroundhttps://wordpress.org/support/topic/remove-previous-next-post-background/
-
How do I display product category icons?https://wordpress.org/support/topic/how-do-i-display-product-category-icons/
-
How to turn OFF the Previous/Next links at post bottom?https://wordpress.org/support/topic/how-to-turn-off-the-previousnext-links-at-post-bottom/
-
Hide tags on postshttps://wordpress.org/support/topic/hide-tags-on-posts/
-
change list-style into pictureshttps://wordpress.org/support/topic/change-list-style-into-pictures/
-
Post navigation within categoryhttps://wordpress.org/support/topic/post-navigation-within-category/
-
Include title attribute in featured imagehttps://wordpress.org/support/topic/include-title-attribute-in-featured-image/
-
Hide featured image if category, if singlehttps://wordpress.org/support/topic/hide-featured-image-if-category-if-single/
-
Next post / last post different color for each category?https://wordpress.org/support/topic/next-post-last-post-different-color-for-each-category/
-
Add subscript to website titlehttps://wordpress.org/support/topic/add-subscript-to-website-title/
-
Not sure how to use post templateshttps://wordpress.org/support/topic/not-sure-how-to-use-post-templates/
-
How to add content after post thumbnail?https://wordpress.org/support/topic/how-to-add-content-after-post-thumbnail/