详解WordPress中调用评论模板和循环输出评论的PHP函数
<p><strong>comments_template</strong><br>
comments_template 函数是一个调用评论模板的函数,使用起来很简单,与get_header()等函数一样,是一个include文件类函数,今天来讲一下他的使用。</p>
<p>
描述<br>
上面已经讲过了,就是调用评论模板的一个函数。</p>
<p>
使用</p>
<div>
<div>
<div id="highlighter_224514">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
</td>
<td>
<div>
<div>
<code><?php comments_template( </code><code>$file</code><code>, </code><code>$separate_comments</code> <code>); ?></code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
其中</p>
<p>
$file 需要调用的文件名 默认值: /comments.php<br>
$separate_comments 是否分隔不用类型的评论 布尔型 默认值: false<br>
讲的比较笼统,更深层次请往下看</p>
<p>
使用实例</p>
<div>
<div>
<div id="highlighter_444916">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
</td>
<td>
<div>
<div>
<code><?php comments_template(get_post_format().</code><code>'-comment.php'</code><code>, true ); ?></code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
这样,就可以根据你不同的文章类型去显示文章评论了。<br>
用法上是不是跟get_template_part()有点类似呢?<br>
至于$separate_comments准确的意思,我也没搞明白,不过官方默认主题一直都是true的所以我也就跟进了。之前研究过一次,貌似这里指的类型是是否回复的意思。</p>
<p>
<strong>wp_list_comments</strong><br>
wp_list_comments 函数是一个循环输出当前文章或页面每个评论的函数,在 WordPress 主题中主要被用来输出每一条评论,省去我们手工便利之苦,也把 WordPress 的评论功能很好的进行了模块化。</p>
<p>
wp_list_comments函数在主题中配合comments_template函数联合使用可以很好的将 WordPress 的评论功能独立出来,而且可以更好对评论嵌套层数、每页显示评论数量、评论样式等等等等进行控制。所以如果你想做好主题的话,那还是看一下吧。</p>
<p>
描述<br>
英文原文:</p>
<p>
Displays all comments for a post or Page based on a variety of parameters including ones set in the administration area.</p>
<p>
我的理解:<br>
用一个整体设置的参数来显示一篇文章、页面的所有评论。</p>
<p>
用法</p>
<div>
<div>
<div id="highlighter_605641">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
<div>
2</div>
<div>
3</div>
</td>
<td>
<div>
<div>
<code><?php</code>
</div>
<div>
<code> </code><code>wp_list_comments( </code><code>$args</code> <code>); </code>
</div>
<div>
<code>?></code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
参数使用<br>
</p>
<div>
<div>
<div id="highlighter_762751">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
<div>
2</div>
<div>
3</div>
<div>
4</div>
<div>
5</div>
<div>
6</div>
<div>
7</div>
<div>
8</div>
<div>
9</div>
<div>
10</div>
<div>
11</div>
<div>
12</div>
</td>
<td>
<div>
<div>
<code><?php </code><code>$args</code> <code>= </code><code>array</code><code>(</code>
</div>
<div>
<code> </code><code>'walker'</code> <code>=> null,</code>
</div>
<div>
<code> </code><code>'max_depth'</code> <code>=> ,</code>
</div>
<div>
<code> </code><code>'style'</code> <code>=> </code><code>'ul'</code><code>,</code>
</div>
<div>
<code> </code><code>'callback'</code> <code>=> null,</code>
</div>
<div>
<code> </code><code>'end-callback'</code> <code>=> null,</code>
</div>
<div>
<code> </code><code>'type'</code> <code>=> </code><code>'all'</code><code>,</code>
</div>
<div>
<code> </code><code>'page'</code> <code>=> ,</code>
</div>
<div>
<code> </code><code>'per_page'</code> <code>=> ,</code>
</div>
<div>
<code> </code><code>'avatar_size'</code> <code>=> 32,</code>
</div>
<div>
<code> </code><code>'reverse_top_level'</code> <code>=> null,</code>
</div>
<div>
<code> </code><code>'reverse_children'</code> <code>=> ); ?></code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
$walker 自定义样式类名<br>
$avatar_size 头像大小 Default: 32<br>
$style 评论容器标签可以是 ‘div', ‘ol', or ‘ul',默认值是'ul',如果不是,那你需要像如下那样明确写出。</p>
<div>
<div>
<div id="highlighter_413519">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
</td>
<td>
<div>
<div>
<code><div </code><code>class</code><code>=</code><code>"commentlist"</code><code>><?php wp_list_comments(</code><code>array</code><code>(</code><code>'style'</code> <code>=> </code><code>'div'</code><code>)); ?></div></code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
或</p>
<div>
<div>
<div id="highlighter_999724">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
</td>
<td>
<div>
<div>
<code><ol </code><code>class</code><code>=</code><code>"commentlist"</code><code>><?php wp_list_comments(</code><code>array</code><code>(</code><code>'style'</code> <code>=> </code><code>'ol'</code><code>)); ?></ol></code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
$type 显示何种评论,参数可以是 ‘all'、'comment'、'trackback'、'pingback'、'pings'. ‘pings' 包括'trackback' 和 ‘pingback'.<br>
默认值: ‘all'<br>
$reply_text 回复按钮的文本值,这个较少用不解释 默认值: ‘Reply'<br>
$login_text 登录按钮的文本值,这个较少用不解释 默认值: ‘Log in to Reply'<br>
$callback 评论显示的回调函数,即显示评论主题的函数名称<br>
$end-callback 应该是循环结束后的回调函数,本人未实测,请自行探索。<br>
$reverse_top_level 布尔值,如果设置本参数为真,则先显示最新一条评论,后面的评论按照后台设置显示。<br>
$reverse_children 布尔值,如果设置本参数为真,则先显示最新一条有子评论的评论,后面的评论按照后台设置显示。<br>
实例<br>
这里是官方默认主题 twentyeleven 中的评论显示演示,<br>
具体其他的参数请参照以上介绍自行演示探索。</p>
<div>
<div>
<div id="highlighter_147584">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
<div>
2</div>
<div>
3</div>
<div>
4</div>
<div>
5</div>
<div>
6</div>
<div>
7</div>
<div>
8</div>
<div>
9</div>
<div>
10</div>
<div>
11</div>
<div>
12</div>
</td>
<td>
<div>
<div>
<code><ol </code><code>class</code><code>=</code><code>"commentlist"</code><code>></code>
</div>
<div>
<code><?php</code>
</div>
<div>
<code> </code><code>/* Loop through and list the comments. Tell wp_list_comments()</code>
</div>
<div>
<code> </code><code>* to use twentyeleven_comment() to format the comments.</code>
</div>
<div>
<code> </code><code>* If you want to overload this in a child theme then you can</code>
</div>
<div>
<code> </code><code>* define twentyeleven_comment() and that will be used instead.</code>
</div>
<div>
<code> </code><code>* See twentyeleven_comment() in twentyeleven/functions.php for more.</code>
</div>
<div>
<code> </code><code>*/</code>
</div>
<div>
<code> </code><code>wp_list_comments( </code><code>array</code><code>( </code><code>'callback'</code> <code>=> </code><code>'twentyeleven_comment'</code> <code>) );</code>
</div>
<div>
<code>//twentyeleven_comment 函数在主题文件 "twentyeleven/functions.php"中定义。</code>
</div>
<div>
<code>?></code>
</div>
<div>
<code></ol></code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
頁:
[1]