判断侧边栏小工具是否使用WordPress函数is_active_sidebar

在开发主题的时候,我们可能会使用到侧边栏这种布局方式,让我们的布局更加灵活多变,使用is_active_sidebar函数判断小工具 ...

在开发主题的时候,我们可能会使用到侧边栏这种布局方式,让我们的布局更加灵活多变,使用is_active_sidebar函数判断小工具是否使用,可以帮助我们根据不同情况实现不同显示效果。今天讲讲判断侧边栏小工具是否使用WordPress函数is_active_sidebar。

判断侧边栏小工具是否使用WordPress函数is_active_sidebar (https://www.wpzt.net/) WordPress基础教程 第1张

is_active_sidebar( string|int $index )

官方描述:判断侧边栏小工具是否使用

参数:

$index

(string|int) (必须) 侧边栏的名称、id、或者数字检查

返回值:

(bool) 如果正在使用返回true,否则返回false

函数原型

该函数位于wp-includes/widgets.php文件中

function is_active_sidebar( $index ) {

$index = ( is_int( $index ) ) ? “sidebar-$index” : sanitize_title( $index );

$sidebars_widgets = wp_get_sidebars_widgets();

$is_active_sidebar = ! empty( $sidebars_widgets[ $index ] );

/**

* Filters whether a dynamic sidebar is considered “active”.

*

* @since 3.9.0

*

* @param bool $is_active_sidebar Whether or not the sidebar should be considered “active”.

* In other words, whether the sidebar contains any widgets.

* @param int|string $index Index, name, or ID of the dynamic sidebar.

*/

return apply_filters( ‘is_active_sidebar’, $is_active_sidebar, $index );

}

简单使用

<?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 } ?>

声明 本站上的部份代码及教程来源于互联网,仅供网友学习交流,若您喜欢本文可附上原文链接随意转载。无意侵害您的权益,请发送邮件至 柒主题 或点击右侧 私信:站长 反馈,我们将尽快处理。

给TA打赏
共{{data.count}}人
人已打赏
0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索