我想做的是在主题激活时使用一些默认小部件预先填充侧边栏小部件区域。
if ( ! dynamic_sidebar( 'sidebar' ) ) :
确实添加了小部件,但它没有显示在小部件部分的侧栏中,并且
if ( is_active_sidebar( 'sidebar' ) ) {
如果侧边栏小部件区域中未加载小部件,则此功能不起作用。
我知道这是可能的,但我只是不知道。我用谷歌搜索但没有找到任何解决方案。感谢您提前提供的任何帮助。
从您的回答中不清楚您是否使用after_switch_theme http://adambrown.info/p/wp_hooks/hook/after_switch_theme钩子,但当你需要设置小部件时。
要激活小部件,我建议将其直接写入数据库get_option('sidebars_widgets')
这应该给出一个数组,并将其保存为update_option('sidebars_widgets', $new_activated_widgets)
.
这应该可以帮助您入门。
/**
* set new widgets on theme activate
* @param string $old_theme
* @param WP_Theme $WP_theme
*/
function set_default_theme_widgets ($old_theme, $WP_theme = null) {
// check if the new theme is your theme
// figure it out
var_dump($WP_theme);
// the name is (probably) the slug/id
$new_active_widgets = array (
'sidebar-name' => array (
'widget-name-1',
'widget-name-2',
'widget-name-3',
),
'footer-sidebar' => array(
'widget-name-1',
'widget-name-2',
'widget-name-3',
)
);
// save new widgets to DB
update_option('sidebars_widgets', $new_active_widgets);
}
add_action('after_switch_theme', 'set_default_theme_widgets', 10, 2);
测试过,直接粘贴进去即可functions.php
你的主题。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)