欢迎来到站长天空!

WordPress

当前位置: 主页 > CMS教程 > WordPress

WordPress获取侧边栏函数get_sidebar用法

时间:2025-10-19 15:09:24|栏目:WordPress|点击:

WordPress函数介绍——获取侧边栏函数get_sidebar详解。

WordPress教程

函数原型

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获取搜索表单函数get_search_form用法

栏    目:WordPress

下一篇:WordPress获取评论模板函数comments_template用法

本文标题:WordPress获取侧边栏函数get_sidebar用法

本文地址:https://zz.feitang.co/CMSjiaocheng/25143.html

广告投放 | 联系我们 | 版权申明

申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:257218569 | 邮箱:257218569@qq.com

Copyright © 2018-2025 站长天空 版权所有 Powered by EyouCms冀ICP备14023439号