Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Clarice Yuen
Clarice Yuen
3,880 Points

Cannot get alt text working

This is from the From Bootstrap to Wordpress course.. the video is "Finishing Up Our Bootstrap Slideshow Carousel". No matter how many times I stare at my code, it looks to me like it's the exact same as the code in the video. I can't figure out why my alt text won't show up. Here's my code:

<?php $thumbnail_id = get_post_thumbnail_id(); $thumbnail_url = wp_get_attachment_image_src( $thumbnail_id, 'thumbnail-size', true ); $thumbnail_meta = get_post_meta( $thumbnail_id, '_wp_attachment_image_alt', true ); ?>

    <a href="<?php the_permalink(); ?>"><img src="<?php echo $thumbnail_url[0]; ?>" alt="<?php echo $thumbnail_meta; ?> featured image"></a>
Ari Winokur
Ari Winokur
Courses Plus Student 6,844 Points

It looks like the words "featured image" are extraneous. Try removing them and testing the code again.

Stanley Thijssen
Stanley Thijssen
22,831 Points

The words "featured" and "image" don't break the code here. It just adds featured image behind the meta that should be pulled in.

3 Answers

Stanley Thijssen
Stanley Thijssen
22,831 Points

Do you feature images have an alt set? I cant find anything wrong inside your code.

Clarice Yuen
Clarice Yuen
3,880 Points

Oh, duh! I can't believe I didn't consider that. I did not actually add any alt text so that would certainly make sense. :) Thanks for saving my sanity.

Stanley Thijssen
Stanley Thijssen
22,831 Points

Can you post the code of the whole file? Might i be correct if I think you didn't use this code inside a loop that contains a featured image?

Clarice Yuen
Clarice Yuen
3,880 Points

Here is the whole file (home.php):

<?php get_header(); ?>

<div class="container"> <div class="row"> <div class="col-md-9">

  <div class="page-header">
    <h1><?php wp_title(''); ?></h1>
  </div>

  <?php 

    $args = array(
      'post_type'     => 'post',
      'category_name' => 'featured' 
    );

    $the_query = new WP_Query( $args );

  ?>

  <div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
    <!-- Indicators -->
    <ol class="carousel-indicators">
      <?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

      <li data-target="#carousel-example-generic" data-slide-to="<?php echo $the_query->current_post; ?>" class="<?php if( $the_query->current_post == 0 ):; ?>active<?php endif; ?>"></li>

      <?php endwhile; endif; ?>
    </ol>

    <?php rewind_posts(); ?>

    <!-- Wrapper for slides -->
    <div class="carousel-inner" role="listbox">

      <?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>


      <div class="item <?php if( $the_query->current_post == 0 ):; ?>active<?php endif; ?>">

        <?php 
          $thumbnail_id = get_post_thumbnail_id(); 
          $thumbnail_url = wp_get_attachment_image_src( $thumbnail_id, 'thumbnail-size', true );
          $thumbnail_meta = get_post_meta( $thumbnail_id, '_wp_attachment_image_alt', true );
        ?>

        <a href="<?php the_permalink(); ?>"><img src="<?php echo $thumbnail_url[0]; ?>" alt="<?php echo $thumbnail_meta; ?> featured image"></a>
        <div class="carousel-caption"><?php the_title(); ?></div>
      </div>

      <?php endwhile; endif; ?>

    </div>

    <!-- Controls -->
    <a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
      <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
      <span class="sr-only">Previous</span>
    </a>
    <a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next">
      <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
      <span class="sr-only">Next</span>
    </a>
  </div>

  <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

  <article class="post">
    <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    <p><em>
      By <?php the_author(); ?> 
      on <?php echo the_time('l, F jS, Y'); ?> 
      in <?php the_category( ', ' ); ?>.
      <a href="<?php comments_link(); ?>"><?php comments_number(); ?></a>
    </em></p>

    <?php the_excerpt(); ?>

    <hr>

  </article>

<?php endwhile; else: ?>

  <div class="page-header">
    <h1>Oh no!</h1>
  </div>

  <p>No content is appearing for this page!</p>

<?php endif; ?>

</div>

<?php get_sidebar( 'blog' ); ?>

</div>

<?php get_footer(); ?>

Stanley Thijssen
Stanley Thijssen
22,831 Points

Good! Can you close the topic :D