WordPress函数:get_sidebar
一、函数简介
引入当前主题的模板文件 sidebar.php 。如果使用特定的名字 ($name) ,那么就会引用包含这个特定名字的模板文件 sidebar-{name}.php 。
如果主题没有 sidebar.php 文件,就会引入默认的 wp-includes/theme-compat/sidebar.php 。
<?php get_sidebar( $name ); ?>
二、函数参数
- $name
(string) (可选) 调用 sidebar-name.php. 默认: None
三、函数案例
(一)、404页面
下面的代码是一个简单模板文件,专门用来显示 "HTTP 404: Not Found" 错误的 (这个文件应该包含在你的主题中,名为 404.php)
<h2>Error 404 - Not Found</h2>
<?php get_sidebar(); ?>
四、调试效果
五、源代码
/**
* Loads sidebar template.
*
* Includes the sidebar template for a theme or if a name is specified then a
* specialized sidebar will be included.
*
* For the parameter, if the file is called "sidebar-special.php" then specify
* "special".
*
* @since 1.5.0
* @since 5.5.0 A return value was added.
* @since 5.5.0 The `$args` parameter was added.
*
* @param string|null $name The name of the specialized sidebar. Default null.
* @param array $args Optional. Additional arguments passed to the sidebar template.
* Default empty array.
* @return void|false Void on success, false if the template does not exist.
*/
function get_sidebar( $name = null, $args = array() ) {
/**
* Fires before the sidebar template file is loaded.
*
* @since 2.2.0
* @since 2.8.0 The `$name` parameter was added.
* @since 5.5.0 The `$args` parameter was added.
*
* @param string|null $name Name of the specific sidebar file to use. Null for the default sidebar.
* @param array $args Additional arguments passed to the sidebar template.
*/
do_action( 'get_sidebar', $name, $args );
$templates = array();
$name = (string) $name;
if ( '' !== $name ) {
$templates[] = "sidebar-{$name}.php";
}
$templates[] = 'sidebar.php';
if ( ! locate_template( $templates, true, true, $args ) ) {
return false;
}
}
THE END