Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
    
Why have u defined the param style as yes –  Maruti Mohanty Nov 28 '13 at 13:14
    
that was a accident –  user2960468 Nov 28 '13 at 13:19
    
is meant to be none –  user2960468 Nov 28 '13 at 13:20
add comment

2 Answers 2

up vote 0 down vote accepted

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.

share|improve this answer
    
perfect thank you –  user2960468 Nov 28 '13 at 14:00
add comment

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>";
 }
share|improve this answer
    
That approach shows all the categories. I need to show only the ones associated with the post –  user2960468 Nov 28 '13 at 13:13
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.