How to exclude certain widget from showing up on home/front page? [duplicate]

I’ve found the exact solution to my question here:

https://wordpress.stackexchange.com/a/17687/92505

Here’s the exact code I used after some modification to make it work on my situation.

Add the following code to functions.php where 'sidebar-1' is your sidebar ID.

'recent-posts' is the name of the widget your want yo hide.

12 is the length of the string 'recent-posts'

Hope someone may find it useful.

add_filter( 'sidebars_widgets', 'wpse17681_sidebars_widgets' );
function wpse17681_sidebars_widgets( $sidebars_widgets )
{
    if ( is_home() || is_front_page() /* Or whatever */ ) {
        foreach ( $sidebars_widgets as $sidebar_id => &$widgets ) {
            if ( 'sidebar-1' != $sidebar_id ) {
                continue;
            }
            foreach ( $widgets as $idx => $widget_id ) {
                // There might be a better way to check the widget name
                if ( 0 === strncmp( $widget_id, 'recent-posts', 12 ) ) {
                    unset( $widgets[$idx] );
                }
            }
        }
    }

    return $sidebars_widgets;
}

Leave a Comment