admin管理员组

文章数量:1122846

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?

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?

Share Improve this question edited Jul 26, 2017 at 16:46 mtm asked Jul 26, 2017 at 11:35 mtmmtm 438 bronze badges 11
  • Do you use these linkы inside the Loop as should be the case? – Max Yudin Commented Jul 26, 2017 at 14:00
  • Yes these are the last thing to happen before the loop's endwhile command. – mtm Commented Jul 26, 2017 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 Commented Jul 26, 2017 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 Commented Jul 26, 2017 at 15:43
  • and i get no links at all if using <?php next_post_link('%link', 'Next &rsaquo;', TRUE ); ?> – mtm Commented Jul 26, 2017 at 15:45
 |  Show 6 more comments

1 Answer 1

Reset to default 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.

本文标签: PreviousNext custom post links within custom taxonomy