1

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.

3
  • Why have u defined the param style as yes Commented Nov 28, 2013 at 13:14
  • that was a accident Commented Nov 28, 2013 at 13:19
  • is meant to be none Commented Nov 28, 2013 at 13:20

2 Answers 2

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.

0
0

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>";
 }
1
  • That approach shows all the categories. I need to show only the ones associated with the post Commented Nov 28, 2013 at 13:13

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.