获取侧边栏WordPress函数get_sidebar用法

获取侧边栏WordPress函数get_sidebar用法:

获取侧边栏WordPress函数get_sidebar用法 (https://www.wp-admin.cn/) WordPress使用教程 第1张

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’);?>

相关推荐

发表回复

房先生
我们将24小时内回复。
2024-05-11 05:27:29
您好,有任何疑问请与我们联系!
您的工单我们已经收到,我们将会尽快跟您联系!
[QQ客服]
2781198
加我微信
[电话联系]
13585372227
[电子邮件]
chaoneo@163.com
取消

选择聊天工具: