0

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?

  • Do you use these linkы inside the Loop as should be the case? – Max Yudin Jul 26 '17 at 14:00
  • Yes these are the last thing to happen before the loop's endwhile command. – mtm Jul 26 '17 at 14:56
  • Oh dear! I've missed this. You can't use the hyphen in a taxonomy name! That's may be the reason. Use underscore _. – Max Yudin Jul 26 '17 at 15:10
  • ah - good to know, thanks. however, i've changed the taxonomy to show_status and adjusted all templates accordingly but am seeing the same problem with the above links (obviously altered to match). – mtm Jul 26 '17 at 15:43
  • and i get no links at all if using <?php next_post_link('%link', 'Next &rsaquo;', TRUE ); ?> – mtm Jul 26 '17 at 15:45
0

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.

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.