2

This is an example of register sidebar function from codex. My focus is on the before_title argument. What i want is to add a class to the title(<h3>). From my widget code or may be pass the class name from user input on widget area.

<?php $args = array(
    'name'          => sprintf(__('Sidebar %d'), $i ),
    'id'            => 'sidebar-$i',
    'description'   => '',
    'before_widget' => '<li id="%1$s" class="widget %2$s">',
    'after_widget'  => '</li>',
    'before_title'  => '<h3>',
    'after_title'   => '</h3>' ); 

register_sidebar( $args );

?>

So, How do i add classes dynamically on widget header?

3

Two approaches:

  1. Don't bother

    Just target .widget.{dynamic-class} h3 instead

  2. Use %2$s

    I don't know if this will work, but try:

    'before_title' => '<h3 class="%2$s">'

But personally, I'd go with the former option. You've already got a unique class name; just leverage the element hierarchy to apply styles selectively.

  • 4
    The class name is parsed into 'before_widget' only. – fuxia Jun 17 '12 at 22:01

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

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