Without using a plugin, how do I prevent/exclude the sidebar widget "recent posts" from showing up on the home/front page?
I do want it to show on the sidebar of rest of other archive/post pages.
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It only takes a minute to sign up.
Sign up to join this communityI'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;
}
none
for the widget block when certain condition is met?
strncmp( $widget_id, 'recent-posts', 12 )
string comparison for checking widget name. Was wondering if there's a better way, as the original author of the code mentioned.