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.

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?

share|improve this question

1 Answer 1

up vote 2 down vote accepted

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.

share|improve this answer
4  
The class name is parsed into 'before_widget' only. –  toscho Jun 17 '12 at 22:01

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.