ThemeShaper Forums » Thematic

[closed]

Removing Sidebar on pages with a specific Class with functions.php

(14 posts)
  • Started 5 years ago by theorib
  • Latest reply from helgatheviking
  • This topic is resolved
  1. Hi all!

    I learnd on this great tutorial how to remove the sidebar from certain pages using the functions.php file but I have another challenging (at least for me who am a total PP novice) question.

    Is there a way to write a function to hide the sidebar from all pages that use a specific class?
    It's easy to do it through CSS but that'll be all to easy =)

    Thanks loads in advanced!

    Posted 5 years ago #
  2. Hi,

    yes .. this can be done. Could you please give me some more details?!

    Chris

    Posted 5 years ago #
  3. Hi Chris, thanks for the fast response and for Thematic!

    I was developing using Sandbox and suddenly came upon Thematic and decided to move my whole web-site design effort over to it, it's awesome!

    Basically, I'm designing a personal photography portfolio and I want all pages with my galleries from NextGen Gallery not to have a sidebar.

    What I don't want is edit functions.php every time I create a gallery to have the sidebar excluded. I want it to be "automatic".

    I figured I could do it in two ways:
    1. Excluding the sidebar on all child-pages of portfolio
    2. Excluding the sidebar form all pages with a specific body class

    Adding classes is easy and actually Thematic pretty much generates automatically the classes I need to tag all my child pages so writing a function to not display the sidebar on pages with the class.x would do and could extend to galleries generated on blog posts not only on the portfolio.

    Currently I have this on my functions.php file so I have to add each page manually to it:

    // filter thematic_sidebar() .. not to display the primary and secondary sidebar on selected pages
    function remove_sidebar() {
    	if (is_page(array('published','home','services','about','portfolio','links','contact','copyright-notice'))) {
    		return FALSE;
    	} else {
    		return TRUE;
    	}
    }
    add_filter('thematic_sidebar', 'remove_sidebar');

    Any thoughts?

    Thanks loads!

    Posted 5 years ago #
  4. Hi Theo,

    take a look at this post: Removing sidebar on child pages

    It'll remove the sidebar from the parent and all child pages. If you want to remove the sidebar only for the child pages, remove the first is_page('whatever') part.

    Chris

    Posted 5 years ago #
  5. Hi Chris, thanks for the response!

    I had already looked into that post and tried it but it didn't work :(

    I don't know why!

    I added:

    // Remove sidebar on portfolio child pages
    function remove_sidebar_portfolio() {
        global $post;
    	// We test if we are on the page 'Visualizations'
    	if ( is_page('portfolio') || $post->post_parent == '5' ) {
        // the page is "Visualizations", or the parent of the page is "Visualizations"
    
    		// Yes, we are .. now we switch off the sidebar
    		return FALSE;
    	} else {
    		// we are not .. we leave the switch on
    		return TRUE;
    	}
    }
    // Connect the filter to thematic_sidebar()
    add_filter('thematic_sidebar', 'remove_sidebar_portfolio');

    to my functions.php file and the sidebar is hidden on the parent but not on the child pages.
    is the $post->post_parent == '5' written right? Sorry, I'm a total PHP newbie, should the "5" be different number for me?

    Posted 5 years ago #
  6. Yes .. sorry .. forgot to explain this one.

    You need to change the 5 to the page id of 'Portfolio'.

    Open the page 'Portfolio' in your browser, change to source view, search for 'pageid-', the following number is the one you need.

    Chris

    Posted 5 years ago #
  7. Wow!! Wonderful!!!

    Thanks loads Chris!!

    best,

    Theo

    Posted 5 years ago #
  8. @Chris

    I am using a remove sidebar function with a few conditions to remove the sidebar from various pages (not necessarily child pages) , and am looking for a way to more intelligently handle the styles to widen the content area. Is there a way to add a class to the body tag in the same function so that a single style can be defined in the css? so instead of a very specific class like this:

    body.slug-forum #container {
     width: 960px;
    }
    
    body.slug-forum #content {
     width: 940px;
    }

    It can be more general like this:

    body.wide #container {
     width: 960px;
    }
    
    body.wide #content {
     width: 940px;
    }
    Posted 4 years ago #
  9. So there is conveniently a core hook called body_class. I have added a nested function, but cannot get it to work. Is there an error with my logic or syntax?

    // Remove sidebar on selected pages
    function remove_sidebar() {
        global $post;
    	if ( is_page('images') || $post->post_parent == '8' ) {
    
    		function add_wide_class($class) {
    			$class = 'wide-page';
    			return $class;
    		}
    		add_filter('body_class','add_wide_class');
    
    		return FALSE;
    
    	} else {
    		return TRUE;
    	}
    }
    
    add_filter('thematic_sidebar', 'remove_sidebar');

    Thanks for any help you can provide.

    Posted 4 years ago #
  10. Hi,

    yep .. it can't work because the add_filter for wide-page comes to late. Try this one:

    // Remove sidebar on selected pages
    function remove_sidebar() {
        global $post;
    	if ( is_page('images') || $post->post_parent == '8' ) {
    		return FALSE;
    	} else {
    		return TRUE;
    	}
    }
    
    add_filter('thematic_sidebar', 'remove_sidebar');
    
    function add_wide_class($class) {
        global $post;
    	if ( is_page('images') || $post->post_parent == '8' ) {
    		$class[] = 'wide-page';
    	}
    	return $class;
    }
    add_filter( 'body_class',  'add_wide_class' );

    Chris

    Posted 4 years ago #
  11. Hi Chris, thanks for your reply. I did have this initially but wanted to avoid the duplicate of the conditional statement.

    Since I'm pretty new to WordPress hooks and programming in general, I am looking to accomplish each function in the most logical way, and learning why it is done a particular way. As a point of interest is there a way I can add the body_class filter as part of the one function?

    In this case though I think it would be best to maintain the functions separately so that it can be used for other body classes at a later stage, but use a array instead of duplicating the conditional.

    My logic is to use an array $body_classes to store the values, then assign it in the function rl_body_classes via the body_class filter. I'm currently getting an error (the html below is being output in the body class) but am not sure how to debug. Ive just been using var_dump up until now to print variables to the browser, but since this is a bit more complex I'm not sure the best way to work out what is going on.

    " <b>Warning</b>: join() [function.join]: Invalid arguments passed in <b>/home/rhysl/public_html/4/wp-content/themes/thematic/library/extensions/dynamic-classes.php</b> on line <b>292</b>"

    // Remove sidebar on selected pages
    
    add_filter('thematic_sidebar', 'rl_remove_sidebar');
    
    function rl_remove_sidebar() {
        global $post;
    	if ( is_page('images') || $post->post_parent == '8' ) {
    		$body_classes = array();
    		global $body_classes;
    		$body_classes[] = 'wide-page';
    		return FALSE;
    
    	} else {
    		return TRUE;
    	}
    }
    
    // Add body classes 
    
    add_filter( 'body_class',  'rl_body_classes' );
    
    function rl_body_classes($classes) {
    	global $body_classes;
    	$body_classes = array();
        $classes = implode(" ",$body_classes);
    	return $classes;
    }

    Thanks

    Posted 4 years ago #
  12. Your comment is awaiting moderation.

    I got the removal of the sidebar working but I can’t access the space.

    I’m using this but it doesn’t work.

    body.projectew #container {
    width: 960px;
    }

    body.projectew #content {
    width: 940px;
    }

    Can anyone help?

    Here is my page. http://www.expandingobsessions.com/projectew/

    Posted 3 years ago #
  13. Looks like you are trying to target the ".projectew" class not present in the html.

    .page-id-14 #container

    Targeting the page-id number should override it.

    Posted 3 years ago #
  14. it looks like you aren't using thematic compatible body classes.

    did you add?:

    //Thematic 0.9.7.6+ compatible
    define('THEMATIC_COMPATIBLE_BODY_CLASS', true);
    define('THEMATIC_COMPATIBLE_POST_CLASS', true);
    define('THEMATIC_COMPATIBLE_COMMENT_HANDLING', true);
    define('THEMATIC_COMPATIBLE_COMMENT_FORM', true);
    define('THEMATIC_COMPATIBLE_FEEDLINKS', true);

    you don't have to add them all... but i always do to take full advantage of thematic goodness.

    Posted 3 years ago #

RSS feed for this topic

Topic Closed

This topic has been closed to new replies.