1

I have two sidebars using the register_sidebars function that display each widget as a list item with a class of .box:

<?php if(function_exists('register_sidebars')){

register_sidebars(2, array(
    'name'=>'sidebar%d',
    'id'  =>'sidebar',
    'before_widget'=>'<li class="box">',
    'after_widget'=>'</li><!--box--><!--aside-->',
    'before_title'=>'<h2>',
    'after_title'=>'</h2>',
));
}
?>

So the output goes something like:

<div class="side">
 <ul>
  <li class="box">
   <div class="textwidget">Lorem ipsum</div>
  </li><!--box--><!--aside-->
  <li class="box">
   <h2>Blogroll</h2>
   <ul class="xoxo blogroll">
   <li><a href="http://example.com">Example</a></li>
   </ul>
  </li><!--box--><!--aside-->
 </ul>
</div>

What I need is a conditional function so that any Links widget (or one with a class of .xoxo) in the sidebars will not get a class of .box because I want to style those and only those differently, and in a way that I can simply change certain styles from inside of .box. The main problem is that I don't know what function or variables to target with a filter. The codex doesn't do a good job of classifying less-used global variables and I can't see anything in the Filter Reference that looks like it should help.

  • Take a look at the question at the very top of the related links to the righthand side of this page(Mike's answer specifically) and see if anything there helps. – t31os Nov 30 '11 at 17:16
2

I coded my own answer together using a filter and dynamic_sidebar_params:

<?php

add_filter('dynamic_sidebar_params','io_blogroll_class');

function io_blogroll_class($params){
 $params[0]['before_widget'] =  '<li class="'.$params[0]['widget_name'].' box">';
 $params[0]['after_widget'] =  '</li><!--'.$params[0]['widget_name'].'-->';

    if ($params[0]['widget_name'] == "Links"){
$params[0]['before_widget'] =  '<li class="'.$params[0]['widget_name'].'">';
 $params[0]['after_widget'] =  '</li><!--'.$params[0]['widget_name'].'-->';
}   
return $params;
}

?>

The first lines actually override whatever you programmed register_sidebar() or register_sidebars()to put before and after a widget. So you can safely leave that blank in register_sidebar() if you're using this code. The first lines of the function make the name of the widget (therefore the type) a class of the list item and give a default class of .box to all widgets. The if statement basically says, "If any of the widgets have the class .Links (which they will if they're a blogroll or linklist) change the XHTML list item tags before and after these widgets to use their widget name as their only class."

I hope this code helps a few people who are staring down the same question.

0

You can actually try that using jQuery.

jQuery('.xoxo').parent().removeClass('box');
  • It's not a bad suggestion, but if it really came to that, I'd just list CSS selectors for the default widgets and make a faster page. But this should be possible with the WP API. – Iterate Nov 30 '11 at 7:44

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.