WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Remove "left-sidebar" from body_class() list in one template only (6 posts)

  1. questions4wp
    Member
    Posted 2 years ago #

    I'm working on a site with two templates, one that's called sidebartemplate.php and one that's called page.php. Most of the pages on my site use sidebartemplate.php, only one uses page.php.

    Sidebartemplate.php has the content on the right 70% of the screen, and calls get_sidebar(), which displays the sidebar on the left 30% of the screen. Works fine.

    One page has content which is far too large to fit in the right 70%, but works fine in 100%. So, I went into page.php and removed the get_sidebar() function. Now the sidebar doesn't show up, but the "primary" div still only gets 70% of the space. I went into Inspect Element in Chrome and found that it was pulling the CSS rule for ".left-sidebar #primary", but it should only be pulling from "#primary", since there is no left sidebar on this particular template. It's getting the left-sidebar from the body declaration:

    <body class="page page-id-174 page-template-default logged-in admin-bar single-author singular two-column left-sidebar">

    Which I believe is generated from header.php:
    <body <?php body_class(); ?>>

    So basically I want to modify what body_class() returns based on template. When using sidebartemplate.php, body_class() should return what it's already returning. When using page.php, body_class() should return what it's returning without the two-column or left-sidebar tags.

    Any advice on how to get this to work?

  2. questions4wp
    Member
    Posted 2 years ago #

    Found it, apparently there's no 'easy' way to edit the tags of a template, which is quite odd, in my opinion. I used code from this site, here's the specific code for my issue:

    add_filter('body_class', 'adjust_body_class', 20, 2);
    function adjust_body_class($wp_classes, $extra_classes) { 
    
    if( is_page_template('page.php') ) :
    // Filter the body classes     
    
          foreach($wp_classes as $key => $value) {
          if ($value == 'left-sidebar') unset($wp_classes[$key]);
          }
    
    endif;
    }

    This runs through the keys that are generated (and apparently can't be edited before generation) and just unset it if it happens to be 'left-sidebar', thus allowing me to have two different sets of CSS for two different templates.

  3. AardvarkGirl
    Member
    Posted 2 years ago #

    I want to do this as well. I used the code above and got this error:

    Warning: join() [function.join]: Invalid arguments passed in /home/aardvark/public_html/ratest3.com/wp-includes/post-template.php on line 387
    class="">

    When I ad this line (as shown in the tutorial you found) I no longer get the error...

    // Add the extra classes back untouched
    return array_merge($wp_classes, (array) $extra_classes );

    BUT - the body class of left-sidebar is still being put into the site (and yes I made sure I was looking on a 'page').

    I want to remove the body class .left-sidebar for the entire site... all possible template files.

    I am working with a 2011 child theme http://ratest3.com/sample-page/

  4. alchymyth
    Forum Moderator
    Posted 2 years ago #

    I was looking on a 'page'

    the reply above is talking about a specific page template with the file name 'page.php';

    if you are targeting all (static) pages, try to use:

    if( is_page() ) :

    there is no wildcard for page templates, you would need to reference them all individually.

    I want to remove the body class .left-sidebar for the entire site... all possible template files.

    depending on what you understand under 'template files', possibly remove the conditional statement totally; however in this case you could just use the 'one-cloumn' theme option, and increase the width(?)

  5. AardvarkGirl
    Member
    Posted 2 years ago #

    alchymyth... hmmmmm.

    What would your opinion be if I was building the child theme with 1 constant side bar on the left and one side bar that would be optional per which ever template file they assigned to the page, on the right?

    (thanks for your reply btw)

    Came back to say.. I think just using the one-column would be easiest. :)

  6. alchymyth
    Forum Moderator
    Posted 2 years ago #

    What would your opinion be if I was building the child theme with 1 constant side bar on the left and one side bar that would be optional per which ever template file they assigned to the page, on the right?

    for that idea, check my other article: http://www.transformationpowertools.com/wordpress/page-template-sidebar-overwrite-theme-options-twenty-eleven

    with that coding, you can create two page templates which will have the sidebar independant of the theme options (as long as one of the layout options with sidebar is ticked)

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.