Style wp_list_categories

I think this is a fairly simple question and im sorry if it is but how would I add a <div> to each individual category within this code:

    <?php
$taxonomy = 'category';
// get the term IDs assigned to post.
$post_terms = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
// separator between links
$separator = ',';

if ( !empty( $post_terms ) && !is_wp_error( $post_terms ) ) {

    $term_ids = implode( ',' , $post_terms );
    $terms = wp_list_categories( 'title_li=&style=none&echo=0&taxonomy=' . $taxonomy . '&include=' . $term_ids );
    $terms = rtrim( trim( str_replace( '<br />',  $separator, $terms ) ), $separator );

    // display post categories
    echo  $terms;
}   
?>

I want to add <div class="btn-standard"> to each category. Please note that I only want to display the categories relavent to the post.

Related posts

2 comments

  1. My suggestion.

    $categories = get_the_category();
    $output = '';
    if($categories) {
        $output = "<ul>";
        foreach($categories as $category) {
            $output .= '<li><div class="btn-standard"><a href="'.get_category_link( $category->term_id ).'" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '">'.$category->cat_name.'</a></div></li>';
        }
        $output .= "</ul>";
    }
    echo $output;
    

    You can check Codex for more reference.

  2. Use this approach instead

     $terms = get_terms( $taxonomy );
     $count = count($terms);
     if ( $count > 0 ){
         echo "<ul>";
         foreach ( $terms as $term ) {
            echo "<li><div>" . $term->name . "</div></li>";
         }
         echo "</ul>";
     }
    

Comments are closed.