铉霸 發表於 2023-6-23 00:00:00

WordPress的主题编写中获取头部模板和底部模板

<p>
<strong>get_header()(获取头部)</strong><br>
引入主题的头部模板,默认会引入当前主题目录里的 header.php 文件。如果指定了一个名称,则引入当前主题目录的 header-{name}.php 文件,如果需要引入的文件不存在则引入 wp-includes/theme-compat/header.php 文件。</p>
<p>
用法</p>
<div>
<div>
<div id="highlighter_626962">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
</td>
<td>
<div>
<div>
<code>get_header( </code><code>$name</code> <code>);</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
参数</p>
<p>
$name</p>
<p>
(字符串)(可选)要引入的文件的名称,如果指定则引入 header-{$name}.php 文件。</p>
<p>
例子</p>
<div>
<div>
<div id="highlighter_211937">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
</td>
<td>
<div>
<div>
<code>&lt;?php get_header(); ?&gt;</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
上边的代码将引入当前主题根目录的 header.php 文件。</p>
<div>
<div>
<div id="highlighter_4310">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
</td>
<td>
<div>
<div>
<code>&lt;?php get_header( </code><code>'main'</code> <code>); ?&gt;</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
上边的代码将引入当前主题根目录的 header-main.php 文件。</p>
<div>
<div>
<div id="highlighter_596780">
<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>if</code><code>( is_home() ) get_header( </code><code>'home'</code> <code>);</code>
</div>
<div>
<code>elseif</code><code>( is_404() ) get_header( </code><code>'404'</code> <code>);</code>
</div>
<div>
<code>else</code> <code>get_header();</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
上边的代码会在首页引入当前主题根目录的 header-home.php 文件,404 页引入当前主题根目录的 header-404.php 文件,其它页面将引入当前主题根目录的 header.php 文件。</p>
<p>
其它</p>
<p>
此函数位于:wp-includes/general-template.php</p>
<p>
<br><strong>get_footer()(获取底部)</strong><br>
get_footer() 用来引入底部模板文件。如果指定名称则引入当前主题根目录的 footer-{name}.php 文件,如果不指定则引入当前主题根目录的 footer.php 文件,如果文件不存在则引入 wp-includes/theme-compat/footer.php 文件。</p>
<p>
用法</p>
<div>
<div>
<div id="highlighter_103121">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
</td>
<td>
<div>
<div>
<code>get_footer( </code><code>$name</code> <code>);</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
参数</p>
<p>
$name</p>
<p>
(字符串)(可选)引入模板的名称,如果指定则引入 footer-{$name}.php 文件。</p>
<p>
返回值</p>
<p>
此函数无返回值。</p>
<p>
例子</p>
<p>
引入当前主题根目录的 footer.php 文件:</p>
<div>
<div>
<div id="highlighter_612857">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
</td>
<td>
<div>
<div>
<code>&lt;?php get_footer(); ?&gt;</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
引入当前主题根目录的 footer-new.php 文件:</p>
<div>
<div>
<div id="highlighter_732379">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
</td>
<td>
<div>
<div>
<code>&lt;?php get_footer( </code><code>'new'</code> <code>); ?&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_524145">
<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>if</code><code>( is_404() ) get_footer( </code><code>'404'</code> <code>);</code><code>//如果是 404 页则引入当前主题根目录的 footer-404.php 文件</code>
</div>
<div>
<code>elseif</code><code>( is_home() ) get_footer( </code><code>'home'</code> <code>);</code><code>//如果是首页则引入当前主题根目录的 footer-home.php 文件</code>
</div>
<div>
<code>else</code> <code>get_footer();</code><code>//如果不是首页或者 404 页则引入当前主题根目录的 footer.php 文件</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的主题编写中获取头部模板和底部模板