wordpress侧边栏小工具判断函数is_active_sidebar()

WordPress教程 1559

is_active_sidebar()函数的作用是检测指定的侧边栏小工具是否在使用并返回结果,正在使用就返回true,没有使用就返回false。比如存在ID分别为sidebar-1、sidebar-2、sidebar-3三个侧边位,要判断sidebar-1的侧边栏中是否存在小工具就可以使用wordpress函数is_active_sidebar()

函数代码

1
<?php is_active_sidebar( $index ); ?>
参数说明

$index – 侧边栏的名称或者id,默认值:None

返回值

布尔型(boolean),如果侧边栏在使用返回true,否则返回false

示例

根据侧边栏是否被使用而显示不同的内容

1
2
3
4
5
6
7
8
9
<?php if(is_active_sidebar('left-sidebar')){ ?>
	<ul id="sidebar">
		<?php dynamic_sidebar('left-sidebar'); ?>
	</ul>
<?php }else{ ?>
	<div class="textwidget">
		<p>广告位待租!</p>
	</div>
<?php } ?>

注:left-sidebar是侧边栏ID

源文件

is_active_sidebar()函数位置:wp-includes/widgets.php.

精品推荐: