【问题标题】:Wordpress: Alternative to wp_get_sidebars_widgets for theme developmentWordpress:替代 wp_get_sidebars_widgets 用于主题开发
【发布时间】:2014-11-18 23:35:50
【问题描述】:
wp_get_sidebars_widgets() 对于在侧边栏中获取有关小部件的信息非常有用。但是,WP codex 声明它是一个私有函数,不能用于主题开发......这就是我的具体情况。有哪些替代方案?没有它如何获取小部件信息?我猜想dynamic_sidebar() 和一些 PHP,但我不知道如何,真的。
我想得到的是:
- 小部件 ID(它告诉我使用了哪些小部件)。
- 用户在表单中输入的小部件数据。
谢谢!
【问题讨论】:
标签:
php
wordpress-theming
wordpress
【解决方案1】:
好的,所以我找到了一个解决方案,但它看起来像一个 hack。如果有人知道的更好...
我的侧边栏的 id 是“主页”,我所有的小部件 id 都以我的前缀 nd_home_ 开头:
global $wp_registered_sidebars, $wp_registered_widgets;
ob_start();
dynamic_sidebar('homepage'); //my sidebar id is 'homepage'
$sidebar_contents = ob_get_clean();
$widgetidspart1 = explode('nd_home_',$sidebar_contents); //my widgets ids start with nd_home_
for ($i = 1; $i < count($widgetidspart1); $i++) {
$widgetidspart2 = explode('" ', $widgetidspart1[$i] );
$widgetids[] = $widgetidspart2[0]; //id without nd_home_
}
//add nd_home_ to every widget id
foreach ( $widgetids as $id) {
$widgetids_total[] = 'nd_home_' . $id;
}
//now I have all the ids in $widgetids_total. I can get the widget data:
foreach( $widgetids_total as $id ) {
$option_name = $wp_registered_widgets[$id]['callback'][0]->option_name;
$key = $wp_registered_widgets[$id]['params'][0]['number'];
$widget_data = get_option($option_name);
$data[] = $widget_data[$key];
}
//$data contains my widgets data