• Resolved Tristan

    (@mrt209)


    Hi,

    I need help with the code below.

    <?php if(has_tag(array('Actress','Model','Singer')) ) { ?>
    <li class="occupation">Occupation:
    <?php if(has_tag('Actress')){ ?><a href="https://www.galleryfake.com/tag/Actress">Actress</a><?php } ?>
    <?php if(has_tag('Model')){ ?><a href="https://www.galleryfake.com/tag/Model">Model</a><?php } ?>
    <?php if(has_tag('Singer')){ ?><a href="https://www.galleryfake.com/tag/Singer">Model</a><?php } ?>
    </li>
    <?php } ?>

    The code currently displays this in a post “Occupation: Actress” or “Occupation: Singer”… and then adds a link to the specific tag page.

    The problem I have with this is that if a post has more than 1 of these tags then the spacing is wrong. It displays as “Occupation: ActressSinger” or “Occupation: ActressModel”.

    I want it to display commas in-between the different occupations, so it would show like this “Occupation: Actress, Singer”.

    How could I achieve that?

    Also, is there an easier way to do it than I’m doing it?

    Thanks!

    P.S. – I’ve just started coding so still suck

Viewing 7 replies - 1 through 7 (of 7 total)
  • just an idea:

    <?php if(has_tag(array('Actress','Model','Singer')) ) { ?>
    <li class="occupation">Occupation:
    <?php $comma = ''; if(has_tag('Actress')){ $comma = ', '; ?><a href="https://www.galleryfake.com/tag/Actress">Actress</a><?php } ?>
    <?php if(has_tag('Model')){ echo $comma; $comma = ', '; ?><a href="https://www.galleryfake.com/tag/Model">Model</a><?php } ?>
    <?php if(has_tag('Singer')){ echo $comma; ?><a href="https://www.galleryfake.com/tag/Singer">Singer</a><?php } ?>
    </li>
    <?php } ?>

    i.e. the $comma variable would start empty; a succesful if would set the $comma to ', '; it would be echoed before the next succesful if.

    You could enhance the process a little more by using a few arrays..

    <?php
    // Array of tags to check for
    $find_tags = array( 'Actress', 'Model', 'Singer' );
    
    // Array to store matched tags
    $found_tag = array();
    
    // Seperator that sits between each result
    $seperator = ',';
    
    // If we have any one(or more) of the tags
    if( has_tag( $find_tags ) ) :
    
    	// Loop over the tag array
    	foreach( $find_tags as $p_tag ) :
    
    		// If this is a matching tag add to array
    		if( has_tag( $p_tag ) )
    			$found_tag[] = "<a href='https://example.com/tag/$p_tag'>$p_tag</a>";
    
    		continue;
    	endforeach;
    
    	// Join the results together using the seperator if applicable
    	echo  '<li class="occupation">Occupation: '. implode( $seperator, $found_tag ) . '</li>';
    endif;
    ?>

    If it’s a one time use, not needed, but if you’re going to be using the code all over the place and/or you’re likely to add more tags into the condition then you may find the above a little easier to update..

    There as an example anyway.. ??

    Thread Starter Tristan

    (@mrt209)

    Thanks for the help guys ??

    I’m using alchymyth’s method for now and it works perfectly ?? But, I’d like to use Mark’s method eventually. I tried adding the code, but honestly I have no clue what to do with it ??

    Mark could you show me how I would use the code in the single.php file to get the same effect as alchymyth’s method? I would highly appreciate that ??

    Thanks! ??

    Updated the above post for you..

    Nothing special to do, simply replace the code from before with what i’ve posted… ??

    Thread Starter Tristan

    (@mrt209)

    Awesome thanks Mark! Works perfectly ??

    Man I love this forum ??

    Of course either approach will work, i was simply thinking in terms of making the code easier to re-use and/or add to..

    Happy i was able to help though .. ??

    I also want to have some information display using the has_tag but I am pretty lost.

    If a tag is present I want the following information to show at the end of the post.

    [Code moderated as per the Forum Rules. Please use the pastebin]

    I am using the Semi Logic theme so I think I will be able to put this into a widget to display after the post.

    This is the only fix I can think of to make this information show up at the end of posts only and not on pages.

    Help is appreciated…before brain explodes.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Using has_tag to display certain information’ is closed to new replies.