0

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?

2
  • there is on reason for this to fail if there is more then one tag.
    – Bainternet
    Commented Jul 1, 2011 at 6:39
  • whats your tables.php and books.php like?
    – Bainternet
    Commented Jul 1, 2011 at 6:53

1 Answer 1

1

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';
}
?>
2
  • 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
    Commented Jul 1, 2011 at 7:07
  • I chose to use the Tag titles one. Thank you for taking the time to help me mfields.
    – Kapitol
    Commented Jul 1, 2011 at 7:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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