麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 語言 > PHP > 正文

php中get_adjacent_post函數PHP源碼閱讀筆記

2024-09-04 11:49:21
字體:
來源:轉載
供稿:網友

這個函數是wordpress里的一個函數,作用是獲取相鄰的POST文章。

函數并不大,有效代碼大概只有70行左右,但是里面包含的知識不少,所以專門用一篇文章來解釋一下。

get_adjacent_post函數的源碼位于wp-includes/link-template.php中。

我會通過“//roc:”在引出源碼閱讀筆記。

  1. /** 
  2.  * Retrieve adjacent post. 
  3.  * 
  4.  * Can either be next or previous post. 
  5.  * 
  6.  * @since 2.5.0 
  7.  * 
  8.  * @param bool $in_same_cat Optional. Whether post should be in a same category. 
  9.  * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs. 
  10.  * @param bool $previous Optional. Whether to retrieve previous post.                                                              
  11.  * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists. 
  12.  */ 

【筆記】

上面這一段是函數的介紹信息,這個函數包括三個參數:

1 $in_same_cat參數,表示是否需要在同一category中,默認為false。

2 $excluded_categories參數,用于設置忽略哪些category中的post。可以將category ID組成array或comma-separated list的方式來賦值。

3 $previous參數,表示是否提取前一篇post。默認為true。如果希望提取后一篇post,需則設置為false。

此函數的返回值也有三種情況:

1 返回post object,則表明成功;

2 返回NULL,則表明全局$post未設置;

3 返回空字符串,則表明相應的post不存在。

function get_adjacent_post( $in_same_cat = false, $excluded_categories = "", $previous = true ) {

global $wpdb;

【筆記】

這里聲明了$wpdb全局變量,這個變量其實很有來頭的,它是wordpress自身為開發者提供的公有全局變量,開發者們可以直接利用這個函數來對數據庫進行操作,包括新建、刪除、添加、更新等等。

需要注意的是,如果想使用這個“萬能鑰匙”,需要在自己的函數中向上面這樣聲明一下這個變量。

另外,在正常情況下,$wpdb變量只有權限訪問博客所對應的一個數據庫,對其他數據庫是沒有權限的。

比如想查詢數據庫中的表內容,那么可以這樣:

  1. if ( ! $post = get_post() )        return null; 
  2.     $current_post_date = $post->post_date; 
  3.  
  4.     $join = ""
  5.     $posts_in_ex_cats_sql = ""
  6.     if ( $in_same_cat || ! emptyempty$excluded_categories ) ) { 
  7.         $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"
  8.  
  9.         if ( $in_same_cat ) { 
  10.             if ( ! is_object_in_taxonomy( $post->post_type, "category" ) ) 
  11.                 return ""
  12.             $cat_array = wp_get_object_terms($post->ID, "category"array("fields" => "ids")); 
  13.             if ( ! $cat_array || is_wp_error( $cat_array ) ) 
  14.                 return ""
  15.             $join .= " AND tt.taxonomy = "category" AND tt.term_id IN (" . implode(","$cat_array) . ")";  
  16.         }    
  17.  
  18.         $posts_in_ex_cats_sql = "AND tt.taxonomy = "category""
  19.         if ( ! emptyempty$excluded_categories ) ) { 
  20.             if ( ! is_array$excluded_categories ) ) { 
  21.                 // back-compat, $excluded_categories used to be IDs separated by " and " 
  22.                 if ( strpos$excluded_categories" and " ) !== false ) { 
  23.                     _deprecated_argument( __FUNCTION__"3.3", sprintf( __( "Use commas instead of %s to separate excluded categories." ), ""and"" ) ); 
  24.                     $excluded_categories = explode" and "$excluded_categories ); 
  25.                 } else { 
  26.                     $excluded_categories = explode","$excluded_categories ); 
  27.                 } 
  28.             } 
  29.  
  30.             $excluded_categories = array_map"intval"$excluded_categories ); 
  31.  
  32.             if ( ! emptyempty$cat_array ) ) { 
  33.                 $excluded_categories = array_diff($excluded_categories$cat_array); 
  34.                 $posts_in_ex_cats_sql = ""
  35.             } 
  36.  
  37.             if ( !emptyempty($excluded_categories) ) { 
  38.                 $posts_in_ex_cats_sql = " AND tt.taxonomy = "category" AND tt.term_id NOT IN (" . implode($excluded_categories",") . ")"
  39.             } 
  40.         } 
  41.     } 
  42.  
  43.     $adjacent = $previous ? "previous" : "next"
  44.     $op = $previous ? "<" : ">"
  45.     $order = $previous ? "DESC" : "ASC"
  46.  
  47.     $join  = apply_filters( "get_{$adjacent}_post_join"$join$in_same_cat$excluded_categories ); 
  48.     $where = apply_filters( "get_{$adjacent}_post_where"$wpdb->prepare("WHERE p.post_date $op %s AND p.post_type = %s AND p.post_status = "publish" $posts_in_ex_cats_sql"$current_post_date$post->post_type), $in_same_cat$excluded_categories ); 
  49.     $sort  = apply_filters( "get_{$adjacent}_post_sort""ORDER BY p.post_date $order LIMIT 1" ); 
  50.  
  51.     $query = "SELECT p.id FROM $wpdb->posts AS p $join $where $sort"
  52.     $query_key = "adjacent_post_" . md5($query); 
  53.     $result = wp_cache_get($query_key"counts"); 
  54.     if ( false !== $result ) { 
  55.         if ( $result ) 
  56.             $result = get_post( $result ); 
  57.         return $result
  58.     } 
  59.  
  60.     $result = $wpdb->get_var( $query ); 
  61.     if ( null === $result ) 
  62.         $result = ""
  63.  
  64.     wp_cache_set($query_key$result"counts"); 
  65.     //Vevb.com 
  66.     if ( $result ) 
  67.         $result = get_post( $result ); 
  68.  
  69.     return $result

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 亚洲最大的成人网 | 中文字幕电影免费播放 | 一级做人爱c黑人影片 | 欧美精品18 | 欧美成人精品一区 | 一级电影在线观看 | 韩国精品一区二区三区四区五区 | 国产韩国精品一区二区三区久久 | 成人免费av在线 | 亚洲精品动漫在线观看 | 免费毛片儿 | 依依成人精品视频 | 九九视频久久 | 久久精品一区二区三 | 国产在线观看 | 香蕉国产在线视频 | 草碰人人 | 国产亚洲自拍一区 | 密室逃脱第一季免费观看完整在线 | 视频一区二区久久 | 黄色片在线免费播放 | 欧美日韩亚洲在线观看 | 成人不卡一区二区 | 黄色av网站免费 | 狠狠操天天射 | 精品国产一区二区三区四区阿崩 | av色哟哟| 黄色成人短视频 | 一区二区三区播放 | 国产一区二区在线观看视频 | www.91tv| 久久国产亚洲精品 | av电影在线观看网址 | 嗯哈~不行好大h双性 | 久久精品亚洲精品国产欧美kt∨ | 日韩三级伦理在线观看 | 成人羞羞在线观看网站 | 91九色福利 | 国产亚洲欧美日韩高清 | 国产91精品亚洲精品日韩已满 | 亚洲成人福利在线 |