Support » Plugins » Hacks » the_tags as alt text for images

  • Hello,

    I was recently trying to modify my template to use the tags as the alternative text on images, so I came up with this:

    <img src="<?php echo get_option('home'); ?>/wp-content/themes/themename/images/slideshows/image.jpg" alt="<?php the_tags(); ?>"/>

    But it doesn’t work. It basically shows the text right alongside the images. The same sintaxis does work for the permalink, for example:

    <img src="<?php echo get_option('home'); ?>/wp-content/themes/themename/images/slideshows/image.jpg" alt="<?php the_permalink(); ?>"/>

    So I’m a bit confused. If I were to use the get_the_tags function I’d have to make the call and all, so I don’t know if it would work at all anyway. Does anyone have an idea of what could be done here? Thank you very much in advance! –dubsnipe

Viewing 3 replies - 1 through 3 (of 3 total)
  • The problem is the_tags outputs the tag names surrounded by anchor tags, which I’m guessing then break out of the alt tag.
    As to a solution, off the top of my head, you could output the tags to an array in the header, strip the anchor tags, then output this array into your alt text. May not be the best way to do it though.

    this might work:

    <img src="<?php echo get_option('home'); ?>/wp-content/themes/themename/images/slideshows/image.jpg" alt="<?php
    $posttags = get_the_tags();
    if ($posttags) {
    foreach($posttags as $tag) {
    echo $tag->name . ' ';

    this would have the tag names separated by a single space;
    if you need them comma separated, try and change this line:
    echo $tag->name . ' ';
    to this:
    echo $sep . $tag->name; $sep = ', ';

    It works wonderfully! Thank you so very much!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘the_tags as alt text for images’ is closed to new replies.