Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to figure out a way to include a div(which will contain a background image) for a specific tag. This block of code works but fails when a post has more than 1 tag. I'm using a page called tag.php which is the default template for all tags. I'll have quite a few tags so I don't want to create template files for them all, ie: tag-name.php

    <?php  if( has_tag('books') ) {
      include 'tags/books.php';}
          elseif ( has_tag('tables') ) {
      include 'tags/tables.php';}
    ?>

It's always possible there is an easier way to do this. I essentially need a script that detects which tag is being viewed, as a page, then displays the correct image for it. So when the page for the tag "table" is loaded that tag will always use an image named "table.jpg"

Does anyone have any suggestions or know of a way I can modify the script above to do what I described?

share|improve this question
    
there is on reason for this to fail if there is more then one tag. –  Bainternet Jul 1 '11 at 6:39
    
whats your tables.php and books.php like? –  Bainternet Jul 1 '11 at 6:53
add comment

1 Answer

up vote 1 down vote accepted

Here are 2 solutions:

Using Tag titles

<?php
switch( single_tag_title( '', false ) ) {
    case 'Books' :
        include 'tags/books.php';
        break;
    case 'Tables' :
        include 'tags/tables.php';
        break;
    default :
        break;
}
?>

Using tag slugs:

<?php
if ( is_tag( 'books' ) ) {
    include 'tags/books.php';
}
else if ( is_tag( 'tables' ) ) {
    include 'tags/tables.php';
}
?>
share|improve this answer
    
You might also want to check out my taxonomy images plugin, which provides a ui for this sort of thing: wordpress.org/extend/plugins/taxonomy-images –  mfields Jul 1 '11 at 7:07
    
I chose to use the Tag titles one. Thank you for taking the time to help me mfields. –  Kapitol Jul 1 '11 at 7:13
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.