WordPress函数文档comments_template()
导入comments.php文件 描述 Loads the comment template. For use…
导入comments.php文件
描述
Loads the comment template. For use in single Post and Page displays. Will not work outside of single displays unless $withcomments is set to “1”.
用法
<?php comments_template( $file, $separate_comments ); ?>
参数
$file
(string) (可选) The file to load
默认值: /comments.php
$separate_comments
(boolean) (可选) Whether to separate the comments by comment type.
默认值: false
示例
Default Usage
1
2
3
4
|
/* ———————————-
* wordpress函数 kim收集
* ———————————- */
<?php comments_template(); ?>
|
Alternative Comment Template
On some occasions you may want display your comments differently within your Theme. For this you would build an alternate file (ex. short-comments.php) and call it as follows:
1
2
3
4
|
/* ———————————-
* wordpress函数 kim收集
* ———————————- */
<?php comments_template( ‘/short-comments.php’ ); ?>
|
The path to the file used for an alternative comments template should be relative to the current theme root directory, and include any subfolders. So if the custom comments template is in a folder inside the theme, it may look like this when called:
1
2
3
4
|
/* ———————————-
* wordpress函数 kim收集
* ———————————- */
<?php comments_template( ‘/custom-templates/alternative-comments.php’ ); ?>
|
注意
- 使用到 global: (array) $comment List of comment objects for the current post
- 使用到: $wpdb
- 使用到: $id
- 使用到: $post
- 使用到: $withcomments Will not try to get the comments if the post has none.
源文件
comments_template() 函数的代码位于 wp-includes/comment-template.php
.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
/* ———————————-
* wordpress函数 kim收集
* ———————————- */
/**
* Load the comment template specified in $file.
*
* Will not display the comments template if not on single post or page, or if
* the post does not have comments.
*
* Uses the WordPress database object to query for the comments. The comments
* are passed through the ‘comments_array’ filter hook with the list of comments
* and the post ID respectively.
*
* The $file path is passed through a filter hook called, ‘comments_template’
* which includes the TEMPLATEPATH and $file combined. Tries the $filtered path
* first and if it fails it will require the default comment template from the
* default theme. If either does not exist, then the WordPress process will be
* halted. It is advised for that reason, that the default theme is not deleted.
*
* @uses $withcomments Will not try to get the comments if the post has none.
*
* @since 1.5.0
*
* @global WP_Query $wp_query
* @global WP_Post $post
* @global wpdb $wpdb
* @global int $id
* @global object $comment
* @global string $user_login
* @global int $user_ID
* @global string $user_identity
* @global bool $overridden_cpage
*
* @param string $file Optional. The file to load. Default ‘/comments.php’.
* @param bool $separate_comments Optional. Whether to separate the comments by comment type.
* Default false.
*/
function comments_template( $file = ‘/comments.php’, $separate_comments = false ) {
global $wp_query, $withcomments, $post, $wpdb, $id, $comment, $user_login, $user_ID, $user_identity, $overridden_cpage;
if ( !(is_single() || is_page() || $withcomments) || empty($post) )
return;
if ( empty($file) )
$file = ‘/comments.php’;
$req = get_option(‘require_name_email’);
/*
* Comment author information fetched from the comment cookies.
*/
$commenter = wp_get_current_commenter();
/*
* The name of the current comment author escaped for use in attributes.
* Escaped by sanitize_comment_cookies().
*/
$comment_author = $commenter[‘comment_author’];
/*
* The email address of the current comment author escaped for use in attributes.
* Escaped by sanitize_comment_cookies().
*/
$comment_author_email = $commenter[‘comment_author_email’];
/*
* The url of the current comment author escaped for use in attributes.
*/
$comment_author_url = esc_url($commenter[‘comment_author_url’]);
$comment_args = array(
‘order’ => ‘ASC’,
‘orderby’ => ‘comment_date_gmt’,
‘status’ => ‘approve’,
‘post_id’ => $post->ID,
);
if ( $user_ID ) {
$comment_args[‘include_unapproved’] = array( $user_ID );
} elseif ( ! empty( $comment_author_email ) ) {
$comment_args[‘include_unapproved’] = array( $comment_author_email );
}
$comments = get_comments( $comment_args );
/**
* Filter the comments array.
*
* @since 2.1.0
*
* @param array $comments Array of comments supplied to the comments template.
* @param int $post_ID Post ID.
*/
$wp_query->comments = apply_filters( ‘comments_array’, $comments, $post->ID );
$comments = &$wp_query->comments;
$wp_query->comment_count = count($wp_query->comments);
if ( $separate_comments ) {
$wp_query->comments_by_type = separate_comments($comments);
$comments_by_type = &$wp_query->comments_by_type;
}
$overridden_cpage = false;
if ( ” == get_query_var(‘cpage’) && get_option(‘page_comments’) ) {
set_query_var( ‘cpage’, ‘newest’ == get_option(‘default_comments_page’) ? get_comment_pages_count() : 1 );
$overridden_cpage = true;
}
if ( !defined(‘COMMENTS_TEMPLATE’) )
define(‘COMMENTS_TEMPLATE’, true);
$theme_template = STYLESHEETPATH . $file;
/**
* Filter the path to the theme template file used for the comments template.
*
* @since 1.5.1
*
* @param string $theme_template The path to the theme template file.
*/
$include = apply_filters( ‘comments_template’, $theme_template );
if ( file_exists( $include ) )
require( $include );
elseif ( file_exists( TEMPLATEPATH . $file ) )
require( TEMPLATEPATH . $file );
else // Backward compat code will be removed in a future release
require( ABSPATH . WPINC . ‘/theme-compat/comments.php’);
}
|
相关
- Filter(过滤器): comments_template
- Function(函数): comment_form()
Include Tags
- Function(函数): get_header(),
- Function(函数): get_footer(),
- Function(函数): get_sidebar(),
- Function(函数): get_template_part(),
- Function(函数): get_search_form(),
- Function(函数): comments_template()
- 原文:http://codex.wordpress.org/Function_Reference/comments_template
本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。
评论功能已经关闭!