在自定义主题开发中默认启用小部件

时间:2010-09-17 作者:Volomike

在WP主题构建中,如果侧栏不活动,我可以打开它吗?此外,如果侧栏处于活动状态,并且不包含任何小部件,我可以添加一些吗?

我是一个主题和插件开发人员,但这是我还没有学会做的事情。

1 个回复
最合适的回答,由SO网友:Chris_O 整理而成

@Volomike,

当您在主题功能中注册侧栏时,它将在仪表板中处于活动状态。如果没有小部件添加到侧栏,WordPress将添加默认小部件。您可以通过取消注册来阻止WordPress添加默认小部件:

// Remove WP default Widgets
    // WP 2.8 function using $widget_class
    if (function_exists(\'unregister_widget\')) {
        unregister_widget(\'WP_Widget_Meta\');
        unregister_widget(\'WP_Widget_Search\');
        unregister_widget(\'\');

    // pre WP 2.8 function using $id
    } else {
        unregister_widget_control(\'meta\');
        unregister_widget_control(\'search\');
    }
您还可以预设自己的小部件。

// The following code sample is from The Thematic Theme which is licensed under the GPLv2

    register_sidebar_widget(__(\'Search\', \'thematic\'), \'widget_thematic_search\', null, \'search\');
    unregister_widget_control(\'search\');
    register_sidebar_widget(__(\'Meta\', \'thematic\'), \'widget_thematic_meta\', null, \'meta\');
    unregister_widget_control(\'meta\');
    register_sidebar_widget(array(__(\'RSS Links\', \'thematic\'), \'widgets\'), \'widget_thematic_rsslinks\');
    register_widget_control(array(__(\'RSS Links\', \'thematic\'), \'widgets\'), \'widget_thematic_rsslinks_control\', 300, 90);

    // Pre-set Widgets
    $preset_widgets = array (
        \'primary-aside\'  => array( \'search\', \'pages\', \'categories\', \'archives\' ),
        \'secondary-aside\'  => array( \'links\', \'rss-links\', \'meta\' )
        );

    if ( isset( $_GET[\'activated\'] ) ) {
        update_option( \'sidebars_widgets\', apply_filters(\'thematic_preset_widgets\',$preset_widgets ));
    }
Thematic is actually a great example of all the things that can be done with widgets. Take a look at widgets.php and widgets-extensions.php

结束

相关推荐