I have function. It shows like that.
<div id="sidebar-category"><h2 class="sidebar-title black">Konular</h2> <ul>
<li class="cat-item cat-item-2"><a href="http://www.domain.com/cats/girisimler/" >Girişimler</a>
</li>
<li class="cat-item cat-item-3"><a href="http://www.domain.com/cats/isletme-yonetimi/" >İşletme Yönetimi</a>
</li>
<li class="cat-item cat-item-4"><a href="http://www.domain.com/cats/proje-yonetimi/" >Proje Yönetimi</a>
</li>
<li class="cat-item cat-item-6"><a href="http://www.domain.com/cats/web-dunyasi/" >Web Dünyası</a>
</li>
</ul>
</div>
How can I add class in <ul>
?
This is my function :
function arphabet_widgets_init() {
register_sidebar( array(
'name' => 'Home right sidebar',
'id' => 'home_right_1',
'before_widget' => '<div id="sidebar-category">',
'after_widget' => '</div>',
'before_title' => '<h2 class="sidebar-title black">',
'after_title' => '</h2>',
) );
}
add_action( 'widgets_init', 'arphabet_widgets_init' );
I show HTML with this code
<?php dynamic_sidebar( 'home_right_1' ); ?>
I see you are using WordPress. Can you edit the ul in the widget to add the class manually? If not, you could always use jQuery (present in WordPress) to add a class:
$('#sidebar-category ul').addClass('myClass');
Be sure it loads after the dom is ready:
$(document).ready(function() {
$('#sidebar-category ul').addClass('myClass');
});
Are you just using the default wordpress 'Categories' widget? It looks like you can't directly edit the ul element in anyway. According this: https://github.com/WordPress/WordPress/blob/master/wp-includes/widgets/class-wp-widget-categories.php
You would need to either use jQuery like derekcbaldwin suggested or write your own widget/extend the categories widget.
You can achieve that in following two ways:
USING JQUERY:
jQuery(document).ready(function($) {
$('#sidebar-category ul').addClass('your-class-name');
});
USING PHP OUTPUT BUFFERING:
In your sidebar.php or where you used the sidebar output function dynamic_sidebar( 'your-sidebar' ) use the below given code:
<?php
ob_start();
dynamic_sidebar( 'your-sidebar' );
$sidebar_output = ob_get_clean();
$ul_class = 'your-class-name';
echo apply_filters( 'widget_output', $sidebar_output, $ul_class );
?>
In functions.php file add a filter hook to widget_output that will add class to UL:
<?php
function widget_output_processor( $widget_output, $ul_class ) {
$widget_output = str_replace('<ul>', '<ul class="'.$ul_class.'">', $widget_output);
return $widget_output;
}
add_filter( 'widget_output', 'widget_output_processor', 10, 2 );
?>