Advertisement
campusboy

WP the_adjacent_post_link (modified)

Sep 4th, 2016
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.95 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Выводит ссылку (HTML тег A) на смежные записи (следующая/предыдущая).
  4.  *
  5.  * @param string $course принимает значение next/prev.
  6.  */
  7. function da_the_adjacent_post_link( $course = '' ){
  8.   global $post;
  9.   $course = ( $course == 'prev' ) ? true : false;
  10.   $order  = ( $course ) ? 'DESC' : 'ASC';
  11.  
  12.   $link = get_adjacent_post_link( '%link', '%title', true, '', $course );
  13.  
  14.   if ( ! $link ){
  15.     $term = get_the_terms( $post->ID, 'category' );
  16.     $term = $term[0];
  17.     $article = get_posts([
  18.       'numberposts' => 1,
  19.       'exclude'     => $post->ID,
  20.       'category'    => $term->term_id,
  21.       'order'       => $order
  22.     ]);
  23.    
  24.     if ( empty($article) )
  25.       return false;
  26.     else
  27.       $article = $article[0];
  28.    
  29.     $link = sprintf( '<a href="%s" rel="prev">%s</a>', get_the_permalink($article->ID), $article->post_title );
  30.   }
  31.    
  32.   echo $link;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement