function.php中插件的公共功能内容

时间:2016-04-27 11:58:23

标签: php wordpress function

是否可以在我的子主题中获取插件内的公共函数的内容?如果是的话,该怎么做?

这是插件的代码(简化)

final class DM_Reviews {

    public function review_breakdown() {
        echo '<div class="reviews_breakdown">';
        $this->display_total_reviews_count();
        echo '</div>';
    }    

    public function display_total_reviews_count() {
        echo '<div class="reviews-total-count">' . $this->count_reviews() . '</div>';
    }

    public function count_reviews() {
        global $wpdb, $post;

        $count = $wpdb->get_var(
            $wpdb->prepare(
                "
                SELECT COUNT(meta_value)
                FROM {$wpdb->commentmeta}
                LEFT JOIN {$wpdb->comments} ON {$wpdb->commentmeta}.comment_id = {$wpdb->comments}.comment_ID
                WHERE meta_key = 'dm_rating'
                AND comment_post_ID = %d
                AND comment_approved = '1'
                AND meta_value > 0
                ",
                $post->ID
            )
        );

        return $count;
    }

}   

我想在我的主题中复制这部分

echo '<div class="reviews_breakdown">';
        $this->display_total_reviews_count();
echo '</div>';

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

也许您可以创建一个单独的函数并将其放在functions.php中:

function my_count_reviews() {
    global $wpdb, $post;

    $count = $wpdb->get_var(
        $wpdb->prepare(
            "
            SELECT COUNT(meta_value)
            FROM {$wpdb->commentmeta}
            LEFT JOIN {$wpdb->comments} ON {$wpdb->commentmeta}.comment_id = {$wpdb->comments}.comment_ID
            WHERE meta_key = 'dm_rating'
            AND comment_post_ID = %d
            AND comment_approved = '1'
            AND meta_value > 0
            ",
            $post->ID
        )
    );

    return $count;
}

然后只需调用该函数

echo '<div class="reviews_breakdown">';
echo my_count_reviews();
echo '</div>';