I'm using the code below to show six random tags, adding a class on an <a> tag.

$args = array(
'taxonomy'   => 'post_tag',
'hide_empty' => 0
$tags = get_terms( $args );
$class_name = 'my-custom-class-name';

if ( ! empty( $tags ) && ! is_wp_error( $tags ) ) {
shuffle( $tags );   
foreach( array_slice( $tags, 0, 6 ) as $tag ) {
    printf( '<a class="%1$s" href="%2$s">%3$s</a>',
        sanitize_html_class( $class_name ),
        get_tag_link( $tag->term_id ),
        sprintf( __( '%s') , $tag->name )

I'm also using a plugin called Taxonomy Images. This plugin allows me to place images with my tags.

wp_get_attachment_image() should display the taxonomy image. My problem is that I don't know how to place that line in the code above without messing something up.

The html looks like this:

<a class="class-name" href="taxonomy-url">taxonomy-name<a>

I want to achieve this, with the <img> tag's class being different from the <a> tag's class:

<a class="class-name" href="taxonomy-link"><img src="taxonomy-image-url" class="class-name"> taxonomy-name</a>

closed as off-topic by kaiser May 8 '16 at 19:57

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.


You should you this plug in instead: Category Image

And don't be shy learning and using printf, it's worth.

printf( '<a class="%1$s" href="%2$s"><img class="img-class" src="%4$s" <h3>%3$s</h3></a>',
    sanitize_html_class( $class_name ),
    get_tag_link( $tag->term_id ),
    sprintf( __( '%s') , $tag->name ),
    z_taxonomy_image_url($tag->term_id, 'thumbnail' )

A note fore easier debugging, you can use var_dump($tag) inside the foreach loop to now exactly what $tag object has.

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