WordPress – Adding Bootstrap to wp_list_categories()

Add the following code to your theme’s functions.php file:

// Categories walker
class Walker_Categories_Template extends Walker_Category {  

    function start_lvl(&$output, $depth=1, $args=array()) {  
        $output .= "\n<ul class=\"list-group\">\n";  
    }  

    function end_lvl(&$output, $depth=0, $args=array()) {  
        $output .= "</ul>\n";  
    }  

    function start_el(&$output, $item, $depth=0, $args=array()) {  
        $output .= "<li class=\"list-group-item\"><a href=\"". esc_url( get_category_link( $item->term_id ) ) ."\" title=\"".esc_attr( $item->name )."\"><span class=\"badge pull-right\">".esc_attr($item->count)."</span>".esc_attr( $item->name );
    }  

    function end_el(&$output, $item, $depth=0, $args=array()) {  
        $output .= "</a></li>\n";  
    }  
}Code language: PHP (php)

Use the categories list function like this:

<?php wp_list_categories( array(
	'walker'=> new Walker_Categories_Template
) ); ?> 
Code language: HTML, XML (xml)