wordpress进阶教程(三十三):获取当前文章的前几篇文章和后几篇文章

本站中的教程都是成系列的文章,所以为了提高用户体验,我在每篇教程的后面列出当前文章的前3篇文章和后3篇文章,这个可以当成是相关文章吧。

代码乃是我参考wp自带的get_adjacent_post函数修改而来,get_adjacent_post函数是wordpress用来获取上一篇和下一篇文章的基本函数。哎呀文采不好就不废话了函数代码:

函数作用:获取当前文章同分类下的前几篇文章和后几篇文章

参数:$previous为true时获取之前的文章,为false的时候获取之后的文章

$number为获取文章数量

  1. //author:www.ashuwp.com   
  2. function ashu_get_adjacent_posts( $previous = true, $number = 1 ) {   
  3.   
  4.     //global当前文章变量 $post 和数据库操作类wpdb   
  5.     global $post$wpdb;   
  6.     if ( empty$post ) )   
  7.         return null;   
  8.   
  9.     $current_post_date = $post->post_date;//当前文章的时间   
  10.   
  11.     $join = '';   
  12.     $posts_in_ex_cats_sql = '';   
  13.     //加入表   
  14.     $join = " INNER JOIN $wpdb->term_relationships AS tr ON p.ID = tr.object_id INNER JOIN $wpdb->term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id";   
  15.     //获取当前文章所属分类,可以同属多个分类,如果是自定义的分类法,将category换成对应的分类法即可   
  16.     $cat_array = wp_get_object_terms($post->ID, 'level', array('fields' => 'ids'));   
  17.     $join .= " AND tt.taxonomy = 'level' AND tt.term_id IN (" . implode(',', $cat_array) . ")";   
  18.   
  19.     //判断时间是大于还是小雨   
  20.     $op = $previous ? '<' : '>';   
  21.     //排序   
  22.     $order = $previous ? 'DESC' : 'ASC';   
  23.        
  24.     $where = $wpdb->prepare("WHERE p.post_date $op %s AND p.post_type = %s AND p.post_status = 'publish' "$current_post_date$post->post_type);   
  25.     $sort  = "ORDER BY p.post_date $order LIMIT 0, $number";   
  26.   
  27.     $query = "SELECT p.* FROM $wpdb->posts AS p $join $where $sort";   
  28.     $query_key = 'adjacent_post_' . md5($query);   
  29.     $result = wp_cache_get($query_key, 'counts');   
  30.     if ( false !== $result )   
  31.         return $result;   
  32.   
  33.     $result = $wpdb->get_results("SELECT p.* FROM $wpdb->posts AS p $join $where $sort");   
  34.     if ( null === $result )   
  35.         $result = '';   
  36.     wp_cache_set($query_key$result, 'counts');   
  37.     return $result;   
  38. }  

将该函数放在主题的functions.php文件中即可,调用该函数的时候会返回一个数组,使用示例:

  1. <h4>本篇教程之前的几篇教程是</h4>   
  2.             <ul>   
  3.             <?php   
  4.             $preposts = ashu_get_adjacent_posts(true,3);   
  5.             foreach$preposts as $postt ){   
  6.                 echo '<li><a href="'.get_permalink($postt->ID).'" title="'.$postt->post_title .'">'.$postt->post_title .'</a></li>';   
  7.             };   
  8.             ?>   
  9.             </ul>   
  10.             <h4>本篇教程之后的几篇教程是</h4>   
  11.             <ul>   
  12.             <?php   
  13.             $nextposts = ashu_get_adjacent_posts(false,3);   
  14.             foreach$nextposts as $postt ){   
  15.                 echo '<li><a href="'.get_permalink($postt->ID).'" title="'.$postt->post_title .'">'.$postt->post_title .'</a></li>';   
  16.             };   
  17.             ?>   
  18.             </ul>  

效果预览:

relate

已有19条评论

odayou进行回复 取消回复