Support

Account

Home Forums General Issues Show Taxonomy Image on Posts Reply To: Show Taxonomy Image on Posts

  • So I worked out out to show the Taxonomy image in a single or archive template of a post that belongs to the category..

    $image = get_field('jg_category_tag', 'category_5');
    
        if ( in_category( 'my-category' ) ) {
            echo '<img src="' . $image['url'] . '" /> ';
    }

    So i use a conditional check to see if the post belongs to the category and if it does it displays the category image.

    But now what is the more optimal way of writing the code when I have numerous categories with images and to only display when a post belongs to them.

    The code below would go into the loop but is not efficient…

    // vars
    $image = get_field('jg_category_tag', 'category_5');
    $image7 = get_field('jg_category_tag', 'category_7');
    $image8 = get_field('jg_category_tag', 'category_8');
    
    if ( in_category( 'my-category' ) ) {
        echo '<img src="' . $image['url'] . '" /> ';
     }
    if ( in_category( 'your-category' ) ) {
        echo '<img src="' . $image7['url'] . '" /> ';
    }
    if ( in_category( 'another-category' ) ) {
        echo '<img src="' . $image8['url'] . '" /> ';
    }