首页 > 解决方案 > 如何显示同一类别wordpress的第一条和最后一条记录?

问题描述

我有一个功能,它应该显示指向与拉动记录相同类别的第一个或最后一个记录的链接,但是通过单击“前进”或“后退”按钮,它会在两个记录之间切换,尽管有在 category 中是三个,或者,在一个记录所在的类别中,抛出另一个类别的记录。我需要他,在没有条目之后,他可以从最后一个记录切换到相同类别的第一条记录,从第一条记录切换到最后一条,因为有一个带有相同类别的后退按钮

 function da_the_adjacent_post_link( $course = '' ){
  global $post;
  $course = ( $course == 'prev' ) ? true : false;
  $order  = ( $course ) ? 'DESC' : 'ASC';
  $class  = ( $course ) ? 'prev' : 'next';

  $link = get_adjacent_post_link( '%link', '%title', true, '', $course );

  if ( ! $link ){
    $term = get_the_terms( $post->ID, 'category' );
    $term = $term[0];
    $article = get_posts([
      'post_type' => portfolio,
      'post_per_page' => 1,
      'exclude'     => $post->ID,
      'category'    => $term->term_id,
      'order'       => $order
    ]);

    if ( empty($article) )
      return false;
    else
      $article = $article[0];

    $link = sprintf( '<a href="' . get_the_permalink($article->ID) . '"> ');
  }

  echo $link;

}




if( get_adjacent_post(true,'',true) ) {
  previous_post_link('%link', '<div class="icon-button">');
} else {
  da_the_adjacent_post_link( $course = '' ); ?>
};

if( get_adjacent_post(true, '', false)){
   next_post_link('%link', ' <div class="icon-button">')
} else {
   da_the_adjacent_post_link( $course = '' ); ?>
};

标签: phpwordpresscustom-post-type

解决方案


推荐阅读