自定义帖子类型的WP导航帖子

时间:2015-04-27 18:14:32

标签: php wordpress custom-post-type

我需要在single.php中包含一个导航按钮,其中包含名为“works”的自定义帖子类型的上一篇和下一篇文章。

我包含了这个

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

或者

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

但不要显示任何内容或导航包含所有帖子和页面。例如,只需要将这个CUT的帖子分页,就像旋转木马画廊一样。

3 个答案:

答案 0 :(得分:1)

我想你会搜索这个:

https://codex.wordpress.org/Pagination

此帖子类型的最后帖子的分页。

你不需要特别的“Menü”。

答案 1 :(得分:1)

试试这个

    <?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>';
  }
  ?>

祝你好运。

此致

答案 2 :(得分:0)

如果使用相同的类别不起作用,那么你应该试试这个。将标记“works”添加到自定义帖子类型的帖子中。然后你可以得到这个标签的previous_和next_post_link:

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