0
votes

** Update (with a little snag)...

I got the code working by echoing the taxonomy term. There is one little snag. You cant have more than ONE term selected. Or else it doesn't know what to do. Here is the code.

Get term ready for echo:

<?php $my_terms = get_the_terms( $post->ID, 'YOUR_TERM_HERE' );
if( $my_terms && !is_wp_error( $my_terms ) ) {
    foreach( $my_terms as $term ) {}
} ;?>

Setup your page navigation:

<?php // get_posts in same custom taxonomy
                  $postlist_args = array(
                     'posts_per_page'  => -1,
                     'orderby'         => '',
                     'order'           => 'ASC',
                     'post_type'       => 'YOUR_POST_TYPE_HERE',
                     'YOUR_TERM_HERE'         => array( $term->slug )  // here is your echo

                  ); 
                  $postlist = get_posts( $postlist_args );    
                  // get ids of posts retrieved from get_posts
                  $ids = array();
                  foreach ($postlist as $thepost) {
                     $ids[] = $thepost->ID;
                  }

                  // get and echo previous and next post in the same taxonomy        
                  $thisindex = array_search($post->ID, $ids);
                  $previd = $ids[$thisindex-1];
                  $nextid = $ids[$thisindex+1];
                  if ( !empty($nextid) ) {
                     echo '<a rel="next" href="' . get_permalink($nextid). '"><i class="fa fa-chevron-circle-left" aria-hidden="true"></i></a>';
                  }
                  if ( !empty($previd) ) {
                     echo '<a rel="prev" href="' . get_permalink($previd). '"><i class="fa fa-chevron-circle-right" aria-hidden="true"></i></a>';
                  } ;?>

If someone knows how to do this with more than one taxonomy term. I would realy like to know.

Well, this is probably a hard one to describe but I'll do my best.

I got a custom post type about us and in that CPT I created a hierarchical custom taxonomy our team. I added a couple of items in that custom taxonomy.

  • Team member 1
  • Team member 2
  • etc

What I am trying to do is when you click on Team member 1 there will be a field (div) that displays the name of Team member 2 and is clickable so you can go to the next team member, or back... and so on.

This kind of does something bu it doesn't go thru the custom taxonomy it goes trough the post_type:

 <?php // get_posts in same custom taxonomy
$postlist_args = array(
   'posts_per_page'  => -1,
   'orderby'         => 'menu_order title',
   'order'           => 'ASC',
   'post_type'       => 'over-ons',
   'taxonomy' => 'ons_team'
); 
$postlist = get_posts( $postlist_args );

// get ids of posts retrieved from get_posts
$ids = array();
foreach ($postlist as $thepost) {
   $ids[] = $thepost->ID;
}

// get and echo previous and next post in the same taxonomy        
$thisindex = array_search($post->ID, $ids);
$previd = $ids[$thisindex-1];
$nextid = $ids[$thisindex+1];
if ( !empty($previd) ) {
   echo '<a rel="prev" href="' . get_permalink($previd). '">previous</a>';
}
if ( !empty($nextid) ) {
   echo '<a rel="next" href="' . get_permalink($nextid). '">next</a>';
} ;?>

Original code says:

// get_posts in same custom taxonomy
$postlist_args = array(
   'posts_per_page'  => -1,
   'orderby'         => 'menu_order title',
   'order'           => 'ASC',
   'post_type'       => 'your_custom_post_type',
   'your_custom_taxonomy' => 'your_custom_taxonomy_term'
); 
$postlist = get_posts( $postlist_args );

// get ids of posts retrieved from get_posts
$ids = array();
foreach ($postlist as $thepost) {
   $ids[] = $thepost->ID;
}

// get and echo previous and next post in the same taxonomy        
$thisindex = array_search($post->ID, $ids);
$previd = $ids[$thisindex-1];
$nextid = $ids[$thisindex+1];
if ( !empty($previd) ) {
   echo '<a rel="prev" href="' . get_permalink($previd). '">previous</a>';
}
if ( !empty($nextid) ) {
   echo '<a rel="next" href="' . get_permalink($nextid). '">next</a>';
}

What is my your_custom_taxonomy_term? Because I dont think I have one for my taxonomies?

Normal navigation doesn't work:

<?php posts_nav_link('separator','prelabel','nextlabel'); ?>

Is there anyone who has made something similar or knows where to start...

2

2 Answers

0
votes

Use this

<?php previous_post_link('<div class="prev">%link</div>'); ?> <?php next_post_link('<div class="next">%link</div>'); ?>

The %link will display the name of the next team member

0
votes

See my update in OP for those of you who wish to know.