這個函數是wordpress裡的一個函數,作用是獲取相鄰的POST文章。
函數並不大,有效代碼大概只有70行左右,但是裡面包含的知識不少,所以專門用一篇文章來解釋一下。
get_adjacent_post函數的源碼位於wp-includes/link-template.php中。
我會通過“//roc:”在引出源碼閱讀筆記。
/**
* Retrieve adjacent post.
*
* Can either be next or previous post.
*
* @since 2.5.0
*
* @param bool $in_same_cat Optional. Whether post should be in a same category.
* @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
* @param bool $previous Optional. Whether to retrieve previous post.
* @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
*/
【筆記】
上面這一段是函數的介紹信息,這個函數包括三個參數:
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變量只有權限訪問博客所對應的一個數據庫,對其他數據庫是沒有權限的。
比如想查詢數據庫中的表內容,那麼可以這樣:
if ( ! $post = get_post() ) return null;
$current_post_date = $post->post_date;
$join = "";
$posts_in_ex_cats_sql = "";
if ( $in_same_cat || ! empty( $excluded_categories ) ) {
$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";
if ( $in_same_cat ) {
if ( ! is_object_in_taxonomy( $post->post_type, "category" ) )
return "";
$cat_array = wp_get_object_terms($post->ID, "category", array("fields" => "ids"));
if ( ! $cat_array || is_wp_error( $cat_array ) )
return "";
$join .= " AND tt.taxonomy = "category" AND tt.term_id IN (" . implode(",", $cat_array) . ")";
}
$posts_in_ex_cats_sql = "AND tt.taxonomy = "category"";
if ( ! empty( $excluded_categories ) ) {
if ( ! is_array( $excluded_categories ) ) {
// back-compat, $excluded_categories used to be IDs separated by " and "
if ( strpos( $excluded_categories, " and " ) !== false ) {
_deprecated_argument( __FUNCTION__, "3.3", sprintf( __( "Use commas instead of %s to separate excluded categories." ), ""and"" ) );
$excluded_categories = explode( " and ", $excluded_categories );
} else {
$excluded_categories = explode( ",", $excluded_categories );
}
}
$excluded_categories = array_map( "intval", $excluded_categories );
if ( ! empty( $cat_array ) ) {
$excluded_categories = array_diff($excluded_categories, $cat_array);
$posts_in_ex_cats_sql = "";
}
if ( !empty($excluded_categories) ) {
$posts_in_ex_cats_sql = " AND tt.taxonomy = "category" AND tt.term_id NOT IN (" . implode($excluded_categories, ",") . ")";
}
}
}
$adjacent = $previous ? "previous" : "next";
$op = $previous ? "<" : ">";
$order = $previous ? "DESC" : "ASC";
$join = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_cat, $excluded_categories );
$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 );
$sort = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1" );
$query = "SELECT p.id FROM $wpdb->posts AS p $join $where $sort";
$query_key = "adjacent_post_" . md5($query);
$result = wp_cache_get($query_key, "counts");
if ( false !== $result ) {
if ( $result )
$result = get_post( $result );
return $result;
}
$result = $wpdb->get_var( $query );
if ( null === $result )
$result = "";
wp_cache_set($query_key, $result, "counts");
if ( $result )
$result = get_post( $result );
return $result;
}