0

I want to apply style to widget content of Wordpress but I see widget Content area is not having uniform class. TextWidget are having class "textwidget" and that is uniform but other widgets are having "ul" as element for example and class is also different there.

Can I add any wrapper Or a particular Class around content part of every widget in Wordpress?

I got this:

function my_widget_content_wrap($content) {
    $content = '<div class="widget-content">'.$content.'</div>';
    return $content;
}
add_filter('widget_text', 'my_widget_content_wrap'); 

but it only works for textwidget which I do not need.

1 Answer 1

4

The register_sidebar function has 'before_widget' and 'after_widget' arguments that, by default, add <li> tags with a class of widget around each widget. You can change that in your theme to add an additional <div> around each widget if li.widget is not a good enough CSS Selector or if your theme removes it.

I don't know what options are in the register_sidebar() function for your theme. You would have to change this code to fit your theme.

register_sidebar( array(
    'name'          => __( 'Sidebar name', 'theme_text_domain' ),
    'id'            => 'unique-sidebar-id',
    'description'   => __( 'Sidebar name description', 'theme_text_domain' ),
    'class'         => '',
    'before_widget' => '<li id="%1$s" class="widget %2$s"><div class="widget-content">',
    'after_widget'  => '</div></li>',
    'before_title'  => '<h2 class="widgettitle">',
    'after_title'   => '</h2>'
) );

Edit:

To isolate just the content, you could style everything in the widget and then style the title. For example, in the style sheet, do something like this:

.widget .widgettitle {
    /** Style the widget title. */
}

.widget {
    /** Style the whole widget. */
}

The .widget .wigettitle selector has greater specificity and will override the matching styles in .widget. The net effect is that only the widget content will be affected by the styles that are overridden.

6
  • This solution will add wrapper outside(before) widgettitle. I want wrapper after widgettitle just for content area in every kinds of widget. Commented Nov 7, 2013 at 16:27
  • Have you tried adding the <div class="widget-content"> to the after_title argument? For example: 'after_title' => '</h2><div class="widget-content">'. I didn't test it. Commented Nov 7, 2013 at 16:38
  • I read about this at other places but it gives broken HTML when you do not add title. and sometimes I need to use HTML widget with title. Commented Nov 7, 2013 at 17:36
  • The only other thing I can think of is to jQuery's last-child selector. Commented Nov 7, 2013 at 18:54
  • I added a CSS way to affect only the widget content. See the edit. Commented Nov 7, 2013 at 20:00

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.