简体   繁体   中英

In wordpress, how remove search widget from sidebar

$registered_sidebars = wp_get_sidebars_widgets(); get an output like this

Array
    (
         [sidebar] => Array
            (
                [0] => search-2
                [1] => recent-posts-2
                [2] => recent-comments-2
                [3] => archives-2
                [4] => categories-2
                [5] => meta-2
                [6] => advanced_sidebar_menu_category-2
                [7] => advanced_sidebar_menu-3
            )

    )

i want to remove search-2 from array, attempted like this

unregister_sidebar( 'search-2' ); 

but not works

unregister_sidebar Unregisters sidebars, not sidebar widgets. 'search-2' is not a sidebar, rather a widget.

wp_get_sidebars_widgets lists the active widgets. If you want to remove 'search-2' from the array returned by wp_get_sidebars_widgets simply go the Appearance > Widgets in the dashboard and "delete" the search widget from the sidebar.

Edit: To hide a widget on a specific page you can use a plugin like Display Widgets , Widget Logic , or Jetpack .

Programmatic-ally, there are several options.

You can use either use different sidebars for different pages. You can include a specific sidebar using the get_sidebar .function on the template page. Just put in the alternate sidebar name.

You can also use the 'sidebars_widgets' filter.

add_filter( 'sidebars_widgets', 'disable_search_widget' );

function disable_search_widget( $sidebars_widgets ) {

    if ( is_home() && is_array($sidebars_widgets['primary-widget-area']) ) {
            foreach($sidebars_widgets['primary-widget-area'] as $i => $widget) {
                if(strpos($widget, 'search-2') !== false) {
                    unset($sidebars_widgets['primary-widget-area'][$i]);
                }
            }

    }

    return $sidebars_widgets;
}

Edit 2 Slight change to the code above. Should prevent error.

The above solution is great but if you are not convenient on using php, the easiest way actually is to use css and just hide the search widget :

.widget.widget_search{ display:none; }

So if you want to hide it on homepage only you can use something like this :

body.home .widget.widget_search{ display:none; }

Hope this would help you ;)

Cheers, phpbits

It works for me. Deregister all widgets from sidebar. You have to call it once, for example in after_switch_theme action

add_action('after_switch_theme', 'deregister_sidebar_widgets');
function deregister_sidebar_widgets () {   
    $sidebar_widgets = wp_get_sidebars_widgets();
    if (isset($sidebar_widgets['sidebar'])) {
       foreach($sidebar_widgets['sidebar'] as $i => $widget) {
           unset($sidebars_widgets['sidebar'][$i]);
        }
        wp_set_sidebars_widgets($sidebar_widgets);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM