繁体   English   中英

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

[英]WP navigation post of a 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的职位(例如轮播画廊)。

我想你搜索这个:

https://codex.wordpress.org/Pagination

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

您不需要特殊的“Menü”。

尝试这个

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

祝好运。

问候

如果使用相同的类别不起作用,则应尝试此操作。 将标签“ 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' ); ?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM