更好的WordPress主题

WordPress 获取文章评论函数 get_comments() 的使用方法和参数介绍

摘要:

WordPress get_comments() 是获取文章评论的函数,功能非常强大。

get_comments() 的使用方法:

$lists = get_comments(array(
    // 参数设置
));
foreach ($lists as $key => $comment) {

}

get_comments() 的参数:

@type string       $author_email              Comment author email address. Default empty.
@type string       $author_url                Comment author URL. Default empty.
@type array        $author__in                Array of author IDs to include comments for. Default empty.
@type array        $author__not_in            Array of author IDs to exclude comments for. Default empty.
@type array        $comment__in               Array of comment IDs to include. Default empty.
@type array        $comment__not_in           Array of comment IDs to exclude. Default empty.
@type bool         $count                     Whether to return a comment count (true) or array of
                                              comment objects (false). Default false.
@type array        $date_query                Date query clauses to limit comments by. See WP_Date_Query.
                                              Default null.
@type string       $fields                    Comment fields to return. Accepts 'ids' for comment IDs
                                              only or empty for all fields. Default empty.
@type int          $ID                        Currently unused.
@type array        $include_unapproved        Array of IDs or email addresses of users whose unapproved
                                              comments will be returned by the query regardless of
                                              `$status`. Default empty.
@type int          $karma                     Karma score to retrieve matching comments for.
                                              Default empty.
@type string       $meta_key                  Include comments with a matching comment meta key.
                                              Default empty.
@type string       $meta_value                Include comments with a matching comment meta value.
                                              Requires `$meta_key` to be set. Default empty.
@type array        $meta_query                Meta query clauses to limit retrieved comments by.
                                              See WP_Meta_Query. Default empty.
@type int          $number                    Maximum number of comments to retrieve.
                                              Default empty (no limit).
@type int          $paged                     When used with $number, defines the page of results to return.
                                              When used with $offset, $offset takes precedence. Default 1.
@type int          $offset                    Number of comments to offset the query. Used to build
                                              LIMIT clause. Default 0.
@type bool         $no_found_rows             Whether to disable the `SQL_CALC_FOUND_ROWS` query.
                                              Default: true.
@type string|array $orderby                   Comment status or array of statuses. To use 'meta_value'
                                              or 'meta_value_num', `$meta_key` must also be defined.
                                              To sort by a specific `$meta_query` clause, use that
                                              clause's array key. Accepts 'comment_agent',
                                              'comment_approved', 'comment_author',
                                              'comment_author_email', 'comment_author_IP',
                                              'comment_author_url', 'comment_content', 'comment_date',
                                              'comment_date_gmt', 'comment_ID', 'comment_karma',
                                              'comment_parent', 'comment_post_ID', 'comment_type',
                                              'user_id', 'comment__in', 'meta_value', 'meta_value_num',
                                              the value of $meta_key, and the array keys of
                                              `$meta_query`. Also accepts false, an empty array, or
                                              'none' to disable `ORDER BY` clause.
                                              Default: 'comment_date_gmt'.
@type string       $order                     How to order retrieved comments. Accepts 'ASC', 'DESC'.
                                              Default: 'DESC'.
@type int          $parent                    Parent ID of comment to retrieve children of.
                                              Default empty.
@type array        $parent__in                Array of parent IDs of comments to retrieve children for.
                                              Default empty.
@type array        $parent__not_in            Array of parent IDs of comments *not* to retrieve
                                              children for. Default empty.
@type array        $post_author__in           Array of author IDs to retrieve comments for.
                                              Default empty.
@type array        $post_author__not_in       Array of author IDs *not* to retrieve comments for.
                                              Default empty.
@type int          $post_ID                   Currently unused.
@type int          $post_id                   Limit results to those affiliated with a given post ID.
                                              Default 0.
@type array        $post__in                  Array of post IDs to include affiliated comments for.
                                              Default empty.
@type array        $post__not_in              Array of post IDs to exclude affiliated comments for.
                                              Default empty.
@type int          $post_author               Post author ID to limit results by. Default empty.
@type string|array $post_status               Post status or array of post statuses to retrieve
                                              affiliated comments for. Pass 'any' to match any value.
                                              Default empty.
@type string       $post_type                 Post type or array of post types to retrieve affiliated
                                              comments for. Pass 'any' to match any value. Default empty.
@type string       $post_name                 Post name to retrieve affiliated comments for.
                                              Default empty.
@type int          $post_parent               Post parent ID to retrieve affiliated comments for.
                                              Default empty.
@type string       $search                    Search term(s) to retrieve matching comments for.
                                              Default empty.
@type string|array $status                    Comment stati to limit results by. Accepts an array
                                              or space/comma-separated list of 'hold' (`comment_status=0`),
                                              'approve' (`comment_status=1`), 'all', or a custom
                                              comment status. Default 'all'.
@type string|array $type                      Include comments of a given type, or array of types.
                                              Accepts 'comment', 'pings' (includes 'pingback' and
                                              'trackback'), or anycustom type string. Default empty.
@type array        $type__in                  Include comments from a given array of comment types.
                                              Default empty.
@type array        $type__not_in              Exclude comments from a given array of comment types.
                                              Default empty.
@type int          $user_id                   Include comments for a specific user ID. Default empty.
@type bool|string  $hierarchical              Whether to include comment descendants in the results.
                                              'threaded' returns a tree, with each comment's children
                                              stored in a `children` property on the `WP_Comment`
                                              object. 'flat' returns a flat array of found comments plus
                                              their children. Pass `false` to leave out descendants.
                                              The parameter is ignored (forced to `false`) when
                                              `$fields` is 'ids' or 'counts'. Accepts 'threaded',
                                              'flat', or false. Default: false.
@type string       $cache_domain              Unique cache key to be produced when this query is stored in
                                              an object cache. Default is 'core'.
@type bool         $update_comment_meta_cache Whether to prime the metadata cache for found comments.
                                              Default true.
@type bool         $update_comment_post_cache Whether to prime the cache for comment posts.
                                              Default false.

本文属原创,转载请注明原文:https://themebetter.com/get_comments.html

感觉很棒,欢迎打赏~

7 打赏
分享到:

评论 (0)

登录后评论
注:有关主题的售前售后请 提交工单

建站!你有充足的理由选择我们

世界上超过43%的网站是由WordPress搭建,而我们是国内最靠谱的WordPress主题开发商
QQ客服 微信客服themebetter微信客服 在线工单 公众号themebetter微信公众号