PHP HTML CSS JS
Click to copy

Paste into your child theme's functions.php file.

/**
 * Manually add sidebar to different pages around the site.
 *
 * @since 1.0
 */

function md_child_theme_template_redirect() {
	// ADDS sidebar to author page
	if ( is_author() )
		add_filter( 'md_filter_has_sidebar', '__return_true' );

	// REMOVES sidebar from specific category
	if ( is_category( 'ID' ) )
		add_filter( 'md_filter_has_sidebar', '__return_false' );
}

add_action( 'template_redirect', 'md_child_theme_template_redirect' );
Click to copy
Click to copy
Click to copy

Filter: Add/remove sidebars

The md_filter_has_sidebar filter allows you to bypass the MD sidebar controls and conditionally add/remove the sidebar on any page.

By loading the filter through template_redirect we can use a WordPress conditional tag to ensure the filter only fires on the page(s) we want it to.

When using the filter you only need to choose the conditions and return a value of true or false to add/remove the sidebar from the given page. This would usually be done by creating a function with either return value, but we can instead use the native WordPress return functions to save us some code:

  • __return_true()
  • __return_false()

This code snippet ADDS the sidebar to author pages and REMOVES the sidebar from a specific post category page. You can repeat this action as many times as you need.

Note: loading this filter without any kind of conditional tag will affect the behavior of your sidebar sitewide so be careful to only use this filter in the right context.

Labs

Get the latest MD sent straight to your inbox

There's always something new in the works in the MD Labs! Join the email list to get the newest content and releases sent straight to your inbox.*

Join the MD Newsletter

* I only email you when there's something new, or to share an exciting MD sneak peak.