一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

php中get_adjacent_post函数PHP源码阅读笔记

时间:2022-06-24 15:10:19 编辑:袖梨 来源:一聚教程网

这个函数是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;
}

热门栏目