Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I would like to add a class to a page template's body if the sidebar is not defined. How can I do that?

share|improve this question
    
Do you have multiple templates for your different pages? Is there a 'sidebar' template, and a 'no-sidebar' template? You are getting answers all along the same lines but say these don't work for you so please try to clarify the question. –  s_ha_dum Dec 30 '12 at 19:17
    
No i have only a page.php with sidebar and about 5 pages without sidebar and unique design. I know i can ceeate templates for pages and i can select it from a select menu when i create the page but created files with name page-(pagename).php is it a template too? –  user25381 Dec 30 '12 at 20:02
    
"page-(pagename).php" would be a template too, sure. @Norcross is getting at the point I was fishing for. If you have templates for these different pages, you are making this much more complicated than it needs to be. –  s_ha_dum Dec 31 '12 at 15:31
add comment

3 Answers

up vote 0 down vote accepted

if you are using an actual page template (as selected in the dropdown panel in the admin area) then using is_page_template('file-name.php') in the body_class function will do it.

then, the CSS would be body.class-name #content would apply to only that template area.

share|improve this answer
    
Yes its true but can is there a way to do that automatically? If get_sidebar exist => addclass has-sidebar –  user25381 Dec 30 '12 at 20:05
add comment

I believe you need is_active_sidebar.

if (!is_active_sidebar('sidebar-name')) $class = 'no-sidebar';

Or possibly is_dynamic_sidebar.

share|improve this answer
    
I mean if the get_sidebar() added to the template. I have pages with sidebar, but I have pages with custom template where get_sidebar() is not defined. (page-[page-name].php) If sidebar not added the left_content need to be 1000px, if added 800 px for left and 200 for right(sidebar field) content. –  user25381 Dec 30 '12 at 18:50
    
I don't think there is a way to do that. I'll edit the answer with that clarification in mind. –  s_ha_dum Dec 30 '12 at 18:54
    
Well... Toscho thinks his code will do that. Does it not? –  s_ha_dum Dec 30 '12 at 19:01
    
So what's the best way? For example I have Support, FAQ, Contact page where I need sidebar and I have few pages where I don't need. Every pages is unique, with unique design and functions. –  user25381 Dec 30 '12 at 19:04
add comment

If you don’t know the names of the sidebars you can use wp_get_sidebars_widgets():

add_filter( 'body_class', 'wpse_77719_sidebar_body_class' );

function wpse_77719_sidebar_body_class( $classes )
{
    $classes[] = wp_get_sidebars_widgets() ? 'has-sidebar' : 'no-sidebar';
    return $classes;
}

If you know the names use is_active_sidebar() in that function like @s_ha_dum suggested.

share|improve this answer
    
I need something like the twentyeleven theme. I can create pages with sidebar, and without sidebar. If no sidebar added the template adds a "singular" class to the body. Thank you! –  user25381 Dec 30 '12 at 18:56
    
This code should work like that: if no sidebar has been defined a class .no-sidebar will be added. –  toscho Dec 30 '12 at 18:57
    
I removed get_sidebar(); from the template file, but your code adds has-sidebar every time. –  user25381 Dec 30 '12 at 19:02
    
get_sidebar() happens long after body_class() has been called. Your sidebar.php should not create any HTML if there are no sidebars, then it will work. –  toscho Dec 30 '12 at 19:09
    
Sorry but i dont understand csn you explain please? –  user25381 Dec 30 '12 at 20:03
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.