Jump to content

printing 'tag' names and giving them a different background color in wordpress/css


Etoile152

Recommended Posts

HI!

 

 

I have looked through the forum but cannot find a similar query.  

 

I hope I can explain this clearly:

 

 I am working in wordpress on my layout.  It is a magazine style thing and I am trying to have a little box above each article excerpt that says which kind of piece it is (ie: essay, photo-story, comment etc..).

 

The kind of piece is defined in the tags (the first tag for each article is going to be one of these).

 

 

I found a snippet that gets the first tag and prints the name... so far so good, and I managed to place all of that into a html span, with a class name so it works... the tag name gets displayed, and I manage to style it...

 

But, what I am trying to achieve is to be able to style tags differently (ex: essay with yellow background, photo-story with blue background etc...)  

 

I would really appreciate any advice you may have on getting this to work.  

 

I thought perhaps getting the tag name and somehow calling in into the span class tag?  Not even sure this is doable... 

 

Other idea was to just get the tag name and then if conditional statement like : if {tag name is x then span class x echo x}

 

or... some kind of switch statement... 

 

But as you can probably tell by now I am not a coder... I'm just dabbling and not very good... so super confused ;-)

 

 

Thanks in advance for the help.

 

Etoile Maya.

 

 

 

 

Here is something I tried but it's not working... gives me error messages//

	<article itemscope itemtype="http://schema.org/Article">
			<?php 
			$posttags = get_the_tags();
$count=0;
if ($posttags) {
  foreach($posttags as $tag) {
    $count++;
    if (1 == $count) {
     '<span class=' . "$tag->name" . '>'  echo $tag->name . ' ';
    }
  }
}
			 
?>			
</span>		
	
Link to comment
Share on other sites

Note that I moved your question to the PHP Coding Help section.

 

 

As for your code, the part which adds the <span> tag isn't formatted correctly. It should look more like this:

echo '<span class="' . $tag->name . '">' . $tag->name . '</span> ';

Note that I moved the close </span> tag inside your foreach loop.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.