2

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

3
  • 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
    Commented Dec 30, 2012 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
    Commented Dec 30, 2012 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
    Commented Dec 31, 2012 at 15:31

4 Answers 4

0

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.

1
  • Yes its true but can is there a way to do that automatically? If get_sidebar exist => addclass has-sidebar
    – user25381
    Commented Dec 30, 2012 at 20:05
1

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.

5
  • 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
    Commented Dec 30, 2012 at 18:56
  • This code should work like that: if no sidebar has been defined a class .no-sidebar will be added.
    – fuxia
    Commented Dec 30, 2012 at 18:57
  • I removed get_sidebar(); from the template file, but your code adds has-sidebar every time.
    – user25381
    Commented Dec 30, 2012 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.
    – fuxia
    Commented Dec 30, 2012 at 19:09
  • Sorry but i dont understand csn you explain please?
    – user25381
    Commented Dec 30, 2012 at 20:03
1

With php we can have this with ob_start() and ob_get_clean() but it is kinda hacky.

Better to use javascript:

if (!document.contains(document.getElementById('secondary'))) {
    document.body.classList.add('no-sidebar');
}

where secondary is your sidebar ID

1
  • Using javascript for this is the ultimate in hacky.
    – TelFiRE
    Commented Dec 13, 2018 at 18:08
0

I believe you need is_active_sidebar.

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

Or possibly is_dynamic_sidebar.

4
  • 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
    Commented Dec 30, 2012 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
    Commented Dec 30, 2012 at 18:54
  • Well... Toscho thinks his code will do that. Does it not?
    – s_ha_dum
    Commented Dec 30, 2012 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
    Commented Dec 30, 2012 at 19:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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