Skip to content

Snippet: Display Custom Sidebar For Specific Page & Child Pages

// Display the 'my_custom_sidebar' for the page with ID of 2 and all children of this page
add_filter( 'totaltheme/sidebars/primary/name', function( $sidebar ) {
	if ( is_page() && ( is_page( '2' ) || '2' == wp_get_post_parent_id( get_the_ID() ) ) ) {
		$sidebar = 'my_custom_sidebar';
	}
	return $sidebar;
} );
All PHP snippets should be added via child theme's functions.php file or via a plugin. We recommend Code Snippets (100% Free) or WPCode (sponsored)
Back To Top