如何检测 WordPress 主题是否有侧边栏
我正在开发一个 WordPress 插件,它需要能够检测当前活动主题是否具有支持小部件的侧边栏,以便它知道是否可以添加小部件。那么,如何检测当前主题是否支持小部件?
I'm developing a wordpress plugin that needs to be able to detect whether the currently active theme has widget-capable sidebars, so that it knows whether it can add widgets or not. So, how do I detect whether the current theme is widget-aware?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您会很高兴听到 WP 使用了几个变量:
$wp_registered_sidebars 和 $wp_registered_widgets
然后,根据 wp-admin/widgets.php:
$sidebars = array_keys($wp_registered_sidebars);
不要忘记,WordPress 本身会这样做 - 如果主题中没有小部件读取侧边栏,widgets.php 页面会告诉您这一点(如果您查看核心中的 wp-admin/widgets.php,您会看到如何,但这两个变量是它的基础)
You'll be glad to hear there's a couple of variables which WP uses:
$wp_registered_sidebars and $wp_registered_widgets
Then, as per wp-admin/widgets.php:
$sidebars = array_keys($wp_registered_sidebars);
Don't forget, WordPress does this itself - if there are no widget-read sidebars in a theme, the widgets.php page tells you so (if you look at wp-admin/widgets.php in the core, you'll see how, but those 2 variables are the basis of it)
如果不安装主题并尝试确定侧边栏和小部件是否真正起作用,就没有什么好方法。我保留了一个备用域,仅用于测试 WordPress 中的主题和小部件。 WordPress 是一个很棒的工具(我用它运行了至少十几个域),但有些主题很不稳定。
另一件要测试的事情是,当您将小部件放入特定侧边栏中时,它们是否正确显示。我发现有几个主题可以将小部件数据从左侧栏复制到右侧栏,反之亦然。
默认侧边栏(未添加任何小部件)与您开始移动小部件时获得的侧边栏完全不同。
欢迎来到实验计算机科学的奇妙世界。
There is no good way without installing the theme and trying it to determine if the sidebars and widgets REALLY work. I keep a spare domain just for testing themes and widgets in WordPress. WordPress is a wonderful tool (I run at least a dozen domains with it), but some themes are flaky.
Another thing to test is whether the widgets appear correctly when you put them in a specific sidebar. I've found several themes that copy the widget data from the left sidebar to the right side bar and visa-versa.
The default sidebars (with no widgets added) are nothing like the side bars that you get when you start moving widgets around.
Welcome to the wonderful world of experimental Computer Science.