Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End WP – Add class to body if sidebar is active

  • This topic is empty.
Viewing 15 posts - 1 through 15 (of 16 total)
  • Author
    Posts
  • #185174
    taxicss
    Participant

    Hi, just a quick question. How do I dynamically add a class of “has_sidebar” to the body if the page has an active sidebar?

    Thanks.

    #185176
    chrisburton
    Participant

    Have you looked at the Codex?

    #185177
    taxicss
    Participant

    @chrisburton

    I did but what I want is just a class so I can style the pages with sidebars and pages without.

    I’ll keep digging. Thanks.

    EDIT:
    Ok, found it.

    #185179
    chrisburton
    Participant

    I did but what I want is just a class so I can style the pages with sidebars and pages without.

    Yeah. The WP Codex will help you with knowing what to use

    I found this within seconds: http://codex.wordpress.org/Function_Reference/is_active_sidebar

    #185181
    taxicss
    Participant

    Now I’m confused. All my pages now have active sidebar since I’m using the page.php. How can I activate sidebar to some specific page only?

    #185183
    chrisburton
    Participant

    I’m not understanding, can you show me code? Elaborate more rather than a sentence or two if you can.

    Edit:

    How can I activate sidebar to some specific page only?

    Custom Post Types?

    #185185
    taxicss
    Participant

    Ok, here’s some of my code…

    pastebin

    I have 6 pages. They are just normal pages, no templates. Under page.php, I have “<?php get_sidebar(); ?>” which activates the sidebar to every page that is being created.
    Now I only want 4 of my pages to have the sidebar?

    #185190
    taxicss
    Participant

    I’m thinking of the conditional tags to target the specific pages like

    if ( is_page( '' ) ) {
    ...
    }
    <code></code>

    but I don’t know what code to put inside to add the sidebar…

    #185191
    chrisburton
    Participant

    I have 6 pages. They are just normal pages, no templates. Under page.php, I have “<?php get_sidebar(); ?>” which activates the sidebar to every page that is being created. Now I only want 4 of my pages to have the sidebar?

    If I were using WordPress, I would honestly just use a CPT with the same setup as page.php except have the sidebar in the CPT template. That would solve your issue.

    #185192
    chrisburton
    Participant

    but I don’t know what code to put inside to add the sidebar…

    is_page(array('about', 'work', 'contact')) {
        sidebar stuff here
    }
    
    #185194
    taxicss
    Participant

    Ah you mean putting this stuff

    // If Dynamic Sidebar Exists
    if (function_exists('register_sidebar')) {
    // Define Sidebar Widget Area 1
    register_sidebar(array(
    'name' =&gt; __('Widget Area 1', 'spiceksa'),
    'description' =&gt; __('Description for this widget-area...', 'spiceksa'),
    'id' =&gt; 'widget-area-1',
    'before_widget' =&gt; '&lt;div id="%1$s" class="%2$s"&gt;',
    'after_widget' =&gt; '&lt;/div&gt;',
    'before_title' =&gt; '&lt;h3&gt;',
    'after_title' =&gt; '&lt;/h3&gt;'
    ));
    // Define Sidebar Widget Area 2
    register_sidebar(array(
    'name' =&gt; __('Widget Area 2', 'spiceksa'),
    'description' =&gt; __('Description for this widget-area...', 'spiceksa'),
    'id' =&gt; 'widget-area-2',
    'before_widget' =&gt; '&lt;div id="%1$s" class="%2$s"&gt;',
    'after_widget' =&gt; '&lt;/div&gt;',
    'before_title' =&gt; '&lt;h3&gt;',
    'after_title' =&gt; '&lt;/h3&gt;'
    ));
    }
    <code></code>

    inside that?

    #185195
    chrisburton
    Participant

    Edit:

    Oh, I didn’t understand what you were implying. Yes, put it inside of that.

    #185196
    Alen
    Participant

    Put this in your functions.php file. Note: Not tested.

    
    /**
     * ----------------------------------------
     *  Adds sidebar name, as class, on the body
     * ----------------------------------------
     */
    add_filter('body_class', 'init_add_sidebar_classes_to_body');
    function init_add_sidebar_classes_to_body($classes = '')
    {
        if ( is_active_sidebar( 'some-sidebar' ) ) {
            $classes[] = 'some-sidebar';
        }
        return $classes;
    }
    
    #185197
    chrisburton
    Participant

    @AlenAbdula Thanks for chiming in! Seemed like the OP wanted two things: add class to the body, only have the sidebar appear on specific pages.

    #185198
    taxicss
    Participant

    @Alen Thanks, yes I was able to add a class to the body but my problem now is activating/adding the sidebar to specific pages.

Viewing 15 posts - 1 through 15 (of 16 total)
  • The forum ‘Back End’ is closed to new topics and replies.