WordPress.org

Forums

display tags as images in content.php (7 posts)

  1. befranga
    Member
    Posted 1 year ago #

    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?

  2. befranga
    Member
    Posted 1 year ago #

    Nobody can help?

  3. stephencottontail
    Member
    Posted 1 year ago #

    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.

  4. befranga
    Member
    Posted 1 year ago #

    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..

  5. stephencottontail
    Member
    Posted 1 year ago #

    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' ) : ?>

  6. befranga
    Member
    Posted 1 year ago #

    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;?>
  7. stephencottontail
    Member
    Posted 1 year ago #

    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; ?>

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags