WordPress.org

Ready to get started?Download WordPress

Forums

the_tags as alt text for images (4 posts)

  1. dubsnipe
    Member
    Posted 3 years ago #

    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

  2. mattdeanuk
    Member
    Posted 3 years ago #

    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.
    Matt

  3. alchymyth
    Forum Moderator
    Posted 3 years ago #

    http://codex.wordpress.org/Function_Reference/get_the_tags

    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 = ', ';

  4. dubsnipe
    Member
    Posted 3 years ago #

    It works wonderfully! Thank you so very much!

Topic Closed

This topic has been closed to new replies.

About this Topic