0

I have a CPT of 'Projects' and a custom taxonomy term tied to that called 'Sections' (basically the same as 'categories' I just find it clearer to use a different term name on a CPT than on standard Posts).

So I access all my Projects via /projects, and only those within the Section named 'Design' via projects/design. All good.

What I need though, is once on an individual Project view, to have Previous/Next links that are tied to the current taxonomy, i.e. I need to somehow pass in to the single-project.php template a way for it to know which taxonomy to stay within when linking to the next/previous Project.

To further complicate, Projects can be in multiple taxonomy terms e.g. in both 'Design' and 'Development'. If a Project is accessed directly without going through a taxonomy page i.e. via example.com/my-project-title, then the next/previous will simply disregard taxonomy and cycle through all Projects.

Does anyone have an idea how to tackle this? Not had this scenario before and it has me beat. Any help is appreciated.

  • 1
    Your primary problem here is ambiguity, e.g. if you arrive on a project page, which taxonomy do you use for the next and previous arrows? Keep in mind http is a stateless protocol – Tom J Nowell Sep 25 '14 at 0:12
0

The Smarter Navigation plugin by scribu has solved this need for me:

https://wordpress.org/plugins/smarter-navigation/

Not completely perfect but recognises which taxonomy term is being used and provides appropriate links.

0

I know this is old, but I was searching for an answer to this myself and could not find anything, so I wanted to share. I actually had to deal with a very similar setup myself recently. The problem with WordPress' built in adjacent posts links is that (as you mention) if your post happens to be in multiple terms, the next and prev links will often jump to one of those other categories instead of staying in the one you want. Here is what I did to fix this:

First part is a handy function that returns ids:

function ss_get_post_ids($term, $taxonomy)
{
    return get_posts(array(
        'post_type' => 'project', // if you are using a CPT, put it here. 
        'numberposts'   => -1,
        'tax_query'     => array(
            array(
                'taxonomy'  => $taxonomy,
                'field'     => 'id',
                'terms'     => is_array($term) ? $term : array($term),
            ),
        ),
        'fields'        => 'ids',
    ));
}

Next, we call it for our desired term and taxonomy:

$mytermid = '5'; // put your current term_id here
$mytaxonomy = 'project-type'; // put your current taxonomy slug here
$relatedposts = ss_get_post_ids($mytermid, $mytaxonomy); // call our function above to get all the ids for this pair

global $post; 
$currentpostID = $post->ID; // get current post ID   
$currentKey = array_search($currentpostID, $relatedposts); //find current ID in our returned list of IDs to use it as base

// below get the previous and next IDs, and loop back to beginning if at end
$before = (isset($relatedposts[$currentKey - 1])) ? $relatedposts[$currentKey - 1] : $relatedposts[count($relatedposts) - 1];
$after = (isset($relatedposts[$currentKey + 1])) ? $relatedposts[$currentKey + 1] : $relatedposts[0];

// get the previous and next permalinks using the IDs above
$term_previous_post_link = get_permalink($before);
$term_next_post_link = get_permalink($after);

Now you can just use $term_previous_post_link and $term_next_post_link in the place of WordPress' own next and prev post links, and it will actually stay in the same term, even if there are multiples. Hope this helps someone else, I struggled to find a solution for quite some time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.