Working with Next and Prev links in Single Taxonomy Templates

I am building an art gallery website using a custom post type “paintings”. There is a custom taxonomy for categories called “painting_cat” where it can be “studio”, “plein air” etc., and another custom taxonomy called “series” so that paintings that are part of a series can be grouped together.

Paintings can be in both a category and a series. I have created an taxonomy archive page that handles the list of paintings in a series.

On my single-painting.php template, I am using next_post_link() and previous_post_link() as navigation to go to the next painting and prev painting. However, when coming from the taxonomy-series.php archive page, these links don’t maintain the query data. They show every painting.

I would like for the next and prev buttons to only show paintings that are in the same series as the taxonomy-series.php page the user just came from.

Is there a way to do this? I am wondering if it has to do with the permalink structure or if there is a naming convention to use for single posts that can be something to the effect of: single-{post-type}-{taxonomy}.php

I have been searching high and low and haven’t found anything that will do exactly what I am trying to accomplish.

1 Answer
1

The next_post_link() and previous_post_link() functions take in_same_term parameter, which is set to FALSE by default. You will find a complete description of the functions here and here.

in_same_term (boolean) (optional)

Indicates whether previous post must be within the same taxonomy term as the current post. If set to ‘true’, only posts from the current taxonomy term will be displayed. If the post is in both the parent and subcategory, or more than one term, the previous post link will lead to the previous post in any of those terms.

Example:

echo previous_post_link( '« %link', '%title', TRUE, ' ', 'series' );

Leave a Comment