GeekerCode更好的WordPress主题,值得信任的WordPress主题开发商

极美的主题、极致的插件

WordPress获取边栏文件名函数:get_sidebar()

摘要:【函数介绍】get_sidebar()函数是用于包含我们主题中的sidebar.php模板文件。你也可以指定自己的边栏文件名,命名格式为:sidebar-{$name}.php。说明:如果主题中没有sidebar.php文件,那么函数会包含

【函数介绍】

get_sidebar()函数是用于包含我们主题中的sidebar.php模板文件。你也可以指定自己的边栏文件名,命名格式为:sidebar-{$name}.php。

说明:如果主题中没有sidebar.php文件,那么函数会包含来自默认主题wp-content/themes/default/sidebar.php的侧边栏文件。

【函数使用】

<?php get_sidebar( $name ); ?>

【参数说明】

$name

(string) (可选) 边栏php文件名sidebar-name.php.

Default: None

【函数实例】

404页面(你主题下的404.php)模板使用get_sidebar()获取边栏文件:

<?php get_header(); ?>
<h2>Error 404 - Not Found</h2>
<?php get_sidebar(); ?>
<?php get_footer(); ?>

调用左边栏与右边栏:

<?php get_header(); ?>
<?php get_sidebar('left'); ?>
<?php get_sidebar('right'); ?>
<?php get_footer(); ?>

不同页面上的不同侧边栏

<?php
if ( is_home() ) :
		get_sidebar('home');
	elseif ( is_404() ) :
		get_sidebar('404');
	else :
		get_sidebar();
endif;
?>

建站!你有充足的理由选择我们

世界上超过30%的网站是由WordPress搭建,而我们是国内最靠谱的WordPress主题开发商
QQ咨询在线咨询问答互助微信号geekercode微信公众号云服务器