诸天寅 發表於 2023-6-23 00:00:00

讲解WordPress中用于获取评论模板和搜索表单的PHP函数

<p>
<strong>comments_template()(获取评论模板)</strong></p>
<p>
comments_template() 函数用来获取评论模板,一般只能用在文章或者页面上,如果不是文章或者页面将无法显示。<br>
用法</p>
<div>
<div>
<div id="highlighter_988871">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
</td>
<td>
<div>
<div>
<code>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</p>
<p>
(字符串)(可选)要评论模板文件。</p>
<p>
默认值:/comments.php(当前主题根目录的 comments.php 文件)。</p>
<p>
$separate_comments</p>
<p>
(布尔)(可选)是否根据评论的类型区分评论。</p>
<p>
默认值:False</p>
<p>
返回值</p>
<p>
此函数无返回值。</p>
<p>
例子</p>
<p>
默认引入当前主题根目录的 comments.php 文件。</p>
<div>
<div>
<div id="highlighter_338780">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
</td>
<td>
<div>
<div>
<code>&lt;?php comments_template(); ?&gt;</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_327594">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
</td>
<td>
<div>
<div>
<code>&lt;?php comments_template( </code><code>'/short-comments.php'</code> <code>); ?&gt;</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
其它</p>
<p>
此函数位于:wp-includes/comment-template.php</p>
<p>
<br><strong>get_search_form()(获取搜索表单)</strong><br>
get_search_form() 函数用来获取搜索表单,搜索表单的代码位于当前主题根目录的 searchform.php 文件。</p>
<p>
用法</p>
<div>
<div>
<div id="highlighter_472052">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
</td>
<td>
<div>
<div>
<code>get_search_form( </code><code>$echo</code> <code>);</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
参数</p>
<p>
$echo</p>
<p>
(布尔)(可选)如果为真则直接打印搜索表单,如果不为真则返回搜索表单的代码。</p>
<p>
默认值:True</p>
<p>
返回值</p>
<p>
(string)如果 $echo 参数为 False,则返回搜索表单的 Html 代码。</p>
<p>
例子</p>
<p>
如果主题根目录没有 searchform.php 文件,则默认为下边的表单代码:</p>
<div>
<div>
<div id="highlighter_348179">
<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>
</td>
<td>
<div>
<div>
<code>&lt;form role=</code><code>"search"</code> <code>method=</code><code>"get"</code> <code>id=</code><code>"searchform"</code> <code>class</code><code>=</code><code>"searchform"</code> <code>action=</code><code>"&lt;?php esc_url( home_url( '/' )); ?&gt;"</code><code>&gt;</code>
</div>
<div>
<code>  </code><code>&lt;div&gt;</code>
</div>
<div>
<code>    </code><code>&lt;label </code><code>class</code><code>=</code><code>"screen-reader-text"</code> <code>for</code><code>=</code><code>"s"</code><code>&gt;&lt;?php _x( </code><code>'Search for:'</code><code>, </code><code>'label'</code> <code>); ?&gt;&lt;/label&gt;</code>
</div>
<div>
<code>    </code><code>&lt;input type=</code><code>"text"</code> <code>value=</code><code>"&lt;?php get_search_query(); ?&gt;"</code> <code>name=</code><code>"s"</code> <code>id=</code><code>"s"</code> <code>/&gt;</code>
</div>
<div>
<code>    </code><code>&lt;input type=</code><code>"submit"</code> <code>id=</code><code>"searchsubmit"</code> <code>value=</code><code>"&lt;?php esc_attr_x( 'Search', 'submit button' ); ?&gt;"</code> <code>/&gt;</code>
</div>
<div>
<code>  </code><code>&lt;/div&gt;</code>
</div>
<div>
<code>&lt;/form&gt;</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
其它</p>
<p>
此函数位于:wp-includes/general-template.php</p>
頁: [1]
查看完整版本: 讲解WordPress中用于获取评论模板和搜索表单的PHP函数