WordPress.org

Support

Support » Themes and Templates » display tags as images in content.php

display tags as images in content.php

  • I want to display for a specific tag a specific image:

    <?php if (has_tag('109')) : ?> <img src="<?php bloginfo('template_url'); ?>/images/icon_baby.png" class="iconc" />
               	<?php elseif (has_tag('118')) : ?> <img src="<?php bloginfo('template_url'); ?>/images/icon_kleinkind.png" class="iconc" />
                <?php elseif (has_tag('127')) : ?> <img src="<?php bloginfo('template_url'); ?>/images/icon_kindergartenkind.png" class="iconc" />

    This is working very well. But if I have more tags, the images doesen’t show!

    <?php elseif (has_tag('109,127')) : ?> <img src="<?php bloginfo('template_url'); ?>/images/icon_baby.png" class="iconc" />
    <img src="<?php bloginfo('template_url'); ?>/images/icon_kleinkind.png" class="iconc" />

    What am I doing wrong?

Viewing 6 replies - 1 through 6 (of 6 total)
  • Nobody can help?

    Moderator stephencottontail

    @stephencottontail

    If you want has_tag() to check for multiple tags, you must pass the list as an array. Right now, your elseif statement is checking whether the post with the ID 127 has the tag with the ID 109, which probably isn’t what you wanted.

    thanks for your replay. You are right. Now I tried with an array

    <?php elseif (has_tag( array( 'Baby', 'Kindergartenkind' ) ) ) : ?>

    And it checks only if one of these tags are in the array. So it displays again only one tag.

    Then I found this Code Snippet:

    <?php the_tags('<img src="'.get_bloginfo("template_url").'/images/icons/','.png" />'); ?>

    But this simply displays all tags as images.

    Well I guess it’s not possible..

    Moderator stephencottontail

    @stephencottontail

    Now that I read your last post, I think I might have misunderstood what you wanted. If you want to check whether a post has both tags, you could do this:

    if ( has_tag( 'Baby' ) && has_tag( 'Kindergartenkind' ) : ?>

    I think that is exactly what I need. Thanks! Just doesn’t display correctly (now I have the same icon twice for some reason?)

    Is this correct?

    <?php if (has_tag('Baby')) : ?>
         <img src="<?php bloginfo('template_url'); ?>/images/mms-baby-icon.svg" onerror="this.onerror=null; this.src='<?php bloginfo('template_url'); ?>/images/icon_baby.png'" class="iconb">
    <?php endif;?>
    
          <?php if (has_tag('Kleinkind')) : ?>
       <img src="<?php bloginfo('template_url'); ?>/images/mms-kleinkind-icon.svg" onerror="this.onerror=null; this.src='<?php bloginfo('template_url'); ?>/images/icon_kleinkind.png'" class="iconb">
    <?php endif;?>
    
          <?php if (has_tag('Kindergartenkind')) : ?>
      <img src="<?php bloginfo('template_url'); ?>/images/mms-kindergartenkind-icon.svg" onerror="this.onerror=null; this.src='<?php bloginfo('template_url'); ?>/images/icon_kindergartenkind.png'" class="iconb">
    <?php endif;?>
    
       <?php if ( has_tag( 'Baby' ) && ( has_tag( 'Kleinkind' ) )): ?>
      <img src="<?php bloginfo('template_url'); ?>/images/mms-baby-icon.svg" onerror="this.onerror=null; this.src='<?php bloginfo('template_url'); ?>/images/icon_baby.png'" class="iconb">
       <img src="<?php bloginfo('template_url'); ?>/images/mms-kleinkind-icon.svg" onerror="this.onerror=null; this.src='<?php bloginfo('template_url'); ?>/images/icon_kleinkind.png'" class="iconb">
    <?php endif;?>
    
        <?php if ( has_tag( 'Kleinkind' ) && (has_tag( 'Kindergartenkind' ) )): ?>
       <img src="<?php bloginfo('template_url'); ?>/images/mms-kleinkind-icon.svg" onerror="this.onerror=null; this.src='<?php bloginfo('template_url'); ?>/images/icon_kleinkind.png'" class="iconb">
      <img src="<?php bloginfo('template_url'); ?>/images/mms-kindergartenkind-icon.svg" onerror="this.onerror=null; this.src='<?php bloginfo('template_url'); ?>/images/icon_kindergartenkind.png'" class="iconb">
    <?php endif;?>
    
       <?php if ( has_tag( 'Baby' ) && (has_tag( 'Kindergartenkind' ) )): ?>
         <img src="<?php bloginfo('template_url'); ?>/images/mms-baby-icon.svg" onerror="this.onerror=null; this.src='<?php bloginfo('template_url'); ?>/images/icon_baby.png'" class="iconb">
      <img src="<?php bloginfo('template_url'); ?>/images/mms-kindergartenkind-icon.svg" onerror="this.onerror=null; this.src='<?php bloginfo('template_url'); ?>/images/icon_kindergartenkind.png'" class="iconb">
    <?php endif;?>
    
      <?php if ( has_tag( 'Baby' ) && (has_tag( 'Kindergartenkind' ) && (has_tag( 'Kleinkind' ) ))) : ?>
      <img src="<?php bloginfo('template_url'); ?>/images/mms-baby-icon.svg" onerror="this.onerror=null; this.src='<?php bloginfo('template_url'); ?>/images/icon_baby.png'" class="iconb">
      <img src="<?php bloginfo('template_url'); ?>/images/mms-kleinkind-icon.svg" onerror="this.onerror=null; this.src='<?php bloginfo('template_url'); ?>/images/icon_kleinkind.png'" class="iconb">
      <img src="<?php bloginfo('template_url'); ?>/images/mms-kindergartenkind-icon.svg" onerror="this.onerror=null; this.src='<?php bloginfo('template_url'); ?>/images/icon_kindergartenkind.png'" class="iconb">
       <?php endif;?>
    Moderator stephencottontail

    @stephencottontail

    You’ll need to use some elseifs in there so PHP parses correctly. Try this:

    <?php if ( has_tag( 'Baby' ) && has_tag( 'Kindergartenkind' ) && has_tag( 'Kleinkind' ) ) : ?>
        ...
    <?php elseif ( has_tag( 'Baby' ) && has_tag( 'Kindergartenkind' ) : ?>
        ...
    <?php elseif ( has_tag( 'Baby' ) && has_tag( 'Kleinkind' ) : ?>
        ...
    <?php elseif ( has_tag( 'Kindergartenkind' ) && has_tag( 'Kleinkind' ) : ?>
        ...
    <?php elseif ( has_tag( 'Baby' ) : ?>
        ...
    <?php elseif ( has_tag( 'Kindergartenkind' ) : ?>
        ...
    <?php else ( has_tag( 'Kleinkind' ) : ?>
        ...
    <?php endif; ?>
Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘display tags as images in content.php’ is closed to new replies.