WordPress函数介绍——获取侧边栏函数get_sidebar详解。
函数原型
get_sidebar函数位于wp-includes/general-template.php第100行左右,函数原型如下:
function get_sidebar( $name = null ) {/** * Fires before the sidebar template file is loaded. * * The hook allows a specific sidebar template file to be used in place of the * default sidebar template file. If your file is called sidebar-new.php, * you would specify the filename in the hook as get_sidebar( 'new' ). * * @since 2.2.0 * @since 2.8.0 $name parameter added. * * @param string|null $name Name of the specific sidebar file to use. null for the default sidebar. */do_action( 'get_sidebar', $name );$templates = array();$name = (string) $name;if ( '' !== $name )$templates[] = "sidebar-{$name}.php";$templates[] = 'sidebar.php';locate_template( $templates, true );}
函数介绍
由上述代码可以看出,get_sidebar函数需要一个字符串类型参数,该参数可以为空,为空时会自动查找主题目录下的sidebar.php文件作为侧边栏,如果主题目录下没有sidebar.php文件将会使用默认的 wp-includes/theme-compat/sidebar.php 文件作为侧边栏。如果传入参数,则会引入sidebar-参数名.php作为侧边栏。
使用方法
<?php get_sidebar();?>
如果使用传入参数daimadog,将会加载主题目录下sidebar-daimadog.php文件作为侧边栏。
<?php get_sidebar('daimadog');?>
WordPress函数IDE
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。