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.

So... I registered my widget areas with this code:

    register_sidebar( array(
    'name' => __( 'Posts Widget Area', 'ikos' ),
    'id' => 'primary-widget-area',
    'class'         => 'post-w',        
    'description' => __( 'Shown only in Blog Posts, Archives, Categories, etc.', 'ikos' ),
    'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
    'after_widget' => '</li>',
    'before_title' => '<h3 class="widget-title">',
    'after_title' => '</h3>',
) );

Note that I added a class. I want to "grab" the class (dynamically) and use the same code for all sidebars...something like:

<div id="sidebar" class="HERE THE CODE THAT GRABS THE CLASS" role="complementary">

But I don't know how to code that tiny thing. Help please :)

EDIT I am using this code to "wrap" my sidebars in functions.php

// Before Sidebar - do_action('st_before_sidebar')

// call up the action
if ( !function_exists( 'before_sidebar' ) ) {

function before_sidebar($columns) {
if (empty($columns)) {
// Set the default
$columns = 'five';
} else {
// Check the function for a returned variable
$columns = $columns;
}

// Apply the markup
echo '<div id="sidebar" class="'.$columns.' columns" role="complementary">';
}
} //endif
// create our hook
add_action( 'ikos_before_sidebar', 'before_sidebar');  



// After Sidebar
if ( !function_exists( 'after_sidebar' ) ) {
function after_sidebar() {
// Additional Content could be added here
   echo '</div><!-- #sidebar -->';
}
} //endif
add_action( 'ikos_after_sidebar', 'after_sidebar');  

EDIT 2

In pages I just call the sidebars get_sidebar('page'); and inside the sidebar-page.php I call the before and after action written in functions. I need to add a dynamic variable for the classes...is there a way to do so?

share|improve this question
    
I don't understand what you are trying to accomplish. –  s_ha_dum Sep 11 '13 at 18:00
    
You've shown your code for registering the widget, can you show us what you are using to display the widget on your site? –  Stephen S. Sep 11 '13 at 18:14
    
I edited my question with the code that wraps and shows my widget areas –  Cynthia Lara Sep 12 '13 at 15:56
    
Where is the ikos_before_sidebar action from? Is it a parent theme or theme framework? Which parent theme is it? –  Charles Clarkson Sep 12 '13 at 16:40

1 Answer 1

Sidebars are stored in a global variable named $wp_registered_sidebars.

if ( ! function_exists( 'before_sidebar' ) ) {

    function before_sidebar( $columns = 'five' ) {
        global $wp_registered_sidebars;

        $class_name = $wp_registered_sidebars[$columns]['class'];

        // Apply the markup
        echo "<div id='sidebar' class='$class_name' role='complementary'>\n";
    }
}

I assumed that $columns contains the sidebar ID. The value passed into before_sidebar() is the name value given to 'id' ('id' => 'primary-widget-area') in the arguments. I say that because naming that variable $columns seems strange.

share|improve this answer
    
I added the code that I am using to wrap and show my widget areas. It is in functions.php, is the code in your answer dynamic? I mean..I need to grab the class of each sidebar but I have 6 sidebars. I'd like to use the same code and just grab a var of each... or something :/ –  Cynthia Lara Sep 12 '13 at 15:58
    
You would call it like this: wpse_113801_add_dyanmic_sidebar( 'primary-widget-area' );. If you want all 6 sidebars on a page just loop through the sidebar ids. –  Charles Clarkson Sep 12 '13 at 16:34
    
In pages I just call the sidebars get_sidebar('page'); and inside the sidebar-page.php I call the before and after action written in functions. I need to add a dynamic variable for the classes...is there a way to do so? –  Cynthia Lara Sep 13 '13 at 15:38
    
Yes, but that necessary information was not included in your question. I'll update my answer. –  Charles Clarkson Sep 13 '13 at 16:20
    
Sorry I will edit my question...I am sorry I am lame expressing myself! heh –  Cynthia Lara Sep 13 '13 at 16:22

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.