cRiSs rOcCa
cRiSs rOcCa

Reputation: 55

WP navigation post of a custom post type

I need to include in single.php a navigation buttons with previous and next post of the custom post type called 'works'.

I include this

<?php echo get_next_posts_link('Go to next post'); ?>
<?php echo get_previous_posts_link('Go to prev post');?>

or this

<?php previous_post_link( $taxonomy = 'works' ); ?>

But don´t show nothing or the navigation include all posts and pages. Only need pagination the post of this CUT like a carousel gallery, for example.

Upvotes: 0

Views: 2265

Answers (3)

Quebrando Cabeca
Quebrando Cabeca

Reputation: 325

Try this

    <?php 
     $term_list = wp_get_post_terms($post->ID, 'TAXONOMY',   array("fields" => "slugs"));
     if (empty($term_list[1])) {
     print_r($term_list[0]);
     $termo = $term_list[0];
     } else {
     print_r($term_list[1]);
     $termo = $term_list[1];
     }

     // get_posts in same custom taxonomy
     $postlist_args = array(
     'posts_per_page'  => -1,
     'orderby'         => 'menu_order title',
     'order'           => 'ASC',
      'post_type'       => 'CUSTOM-POST-TYPE',
     'taxonomy'=>'TAXONOMY',
     'term'=>$termo,
      ); 
     $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>';
  }
  ?>

Good Luck.

Regards

Upvotes: 1

zork media
zork media

Reputation: 972

If using the same category does not work, then you should try this. Add the tag "works" to the posts in your custom post-type. Then you can get the previous_ and next_post_link to this tag:

 <?php previous_post_link( '%link', 'Previous in works', TRUE, ' ', 'post_tag' ); ?>
 <?php next_post_link( '%link', 'Next in works', TRUE, ' ', 'post_tag' ); ?>

Upvotes: 0

va-dev
va-dev

Reputation: 26

I think u search this one:

https://codex.wordpress.org/Pagination

A pagination for the last Posts of this Post Type.

U dosen't need a special "Menü" for this.

Upvotes: 1

Related Questions