Previous/Next custom post links within custom taxonomy

I have seen this issue in quite a few threads so apologies if I've replicated an issue here but I've had no joy in attempting to implement the solutions I have found.

I have a custom post type: show, to which is assigned a custom taxonomy: show_status. This can be set either to current or past.

In the single-show.php template of my theme I have placed the following:

div class="newerlink"p?php next_post_link('%link', 'Next rsaquo;', $in_same_term = true, $excluded_terms = '', $taxonomy = 'show_status' ); ?/p/div
div class="olderlink"p?php previous_post_link('%link', 'lsaquo; Previous', $in_same_term = true, $excluded_terms = '', $taxonomy = 'show_status' ); ?/p/div

I want the user to be able to navigate within shows categorised as current OR as past but not to move from one type to the other.

I have tried several versions of these links and all show either no links at all or links which do not differentiate between the show_status and pass from post to post without stopping. All have been variants on the basic:

div class="newerlink"p?php next_post_link('%link', 'Next rsaquo;', TRUE ); ?/p/div
div class="olderlink"p?php previous_post_link('%link', 'lsaquo; Previous', TRUE ); ?/p/div

I imagine I haven't quite got it right yet. I've also tried this method, which rendered links but didn't respect the change in show_status either.

The loop in single-show.php is as standard in a post template:

?php if (have_posts()) : while (have_posts()) : the_post(); ?
    // content
?php endwhile; ?    
?php else: ?
?php endif; ?

Where different content is needed by category I am using:

?php if (has_term('current', 'show_status')) { ;?

or

?php if (has_term('past', 'show_status')) { ;?

to differentiate.

How can I get this to work as intended?

Topic next-post-link previous-post-link custom-taxonomy custom-post-types Wordpress

Category Web


I managed to solve this, with some steering in the right direction in the comments under my question, by specifying different templates for different taxonomy terms using single-show.php to divert as follows:

if ( have_posts() ) { the_post(); rewind_posts(); }
    if (has_term('current', 'show_status')) {
        include(TEMPLATEPATH . '/single-show-current.php');
    }
    elseif (has_term('past', 'show_status')) {
        include(TEMPLATEPATH . '/single-show-past.php');
    }
    else {
        include(TEMPLATEPATH . '/single-default.php');
    }

Then, in the single-show-current.php and single-show-past.php templates I specified navigation based on term rather than taxonomy, as pointed out by Max, above (thank you for the help getting here).

The solution is based on this code at Bucket Press.

$postlist_args = array(
    'posts_per_page'  => -1,
    'orderby'         => 'menu_order title',
    'order'           => 'ASC',
    'post_type'       => 'show',
    'show_status'    => 'current'
    ); 
    $postlist = get_posts( $postlist_args );
    $ids = array();
    foreach ($postlist as $thepost) {
        $ids[] = $thepost->ID;
    }   
    $thisindex = array_search($post->ID, $ids);
    $previd = $ids[$thisindex-1];
    $nextid = $ids[$thisindex+1];
    if ( !empty($previd) ) {
        echo '<div class="olderlink"><p><a rel="prev" href="' . get_permalink($previd). '">&lsaquo; Previous</a></p></div>';
                        }
    if ( !empty($nextid) ) {
        echo '<div class="newerlink"><p><a rel="next" href="' . get_permalink($nextid). '">Next &rsaquo;</a></p></div>';
    }

Finally, to make this user-proof I installed the radio buttons for taxonomies plugin and applied it to this post type, effectively reducing the number of possible assignations to 1.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.