1

I seem to have a problem trying to display the "alternative text" of the featured image.

Here is the code that I have been working on. Not sure why the alternative text is showing blank.

        <?php 

        $projects = new WP_Query( array( 'post_type' => 'print-billboard' ) );
        if ( $projects->have_posts() ) : while ( $projects->have_posts() ) :
        $projects->the_post();



        ?>

            <?php 
            $featured_image = wp_get_attachment_image_src( get_post_thumbnail_id(), 'large' );
            $featured_image_url = wp_get_attachment_url( get_post_thumbnail_id() );
            $alt = get_post_meta( $featured_image, '_wp_attachment_image_alt', true );
            if ( has_post_thumbnail() ) :

            ?>


            <a href="<?php echo $featured_image_url; ?>" id="post-<?php the_ID(); ?>" <?php post_class( 'four columns project-post fancybox' ) ?> >

                <?php $thumbnail_meta = get_post_meta( $featured_image, '_wp_attachment_image_alt', true ); ?>

                <figure class="full-width featured-image">
                    <img src="<?php echo $featured_image[0]; ?>" data-fancybox-group="gallery" title="Lorem ipsum dolor sit amet" alt="<?php echo $featured_image; ?>" />
                    <div class="overlay">
                    </div>
                </figure>



            </a>

            <?php endif; ?>

        <?php wp_reset_postdata(); ?>

        <?php endwhile; else: ?>

        <p class="error-message">No Projects</p>

        <?php endif; ?>

        <?php wp_reset_query(); ?>
1

I am not so sure of what I am going to say, but from a fast look, $featured_image is an array (URL, width, height, is_intermediate). so assuming that your alternative text exists in this array, you need to specify its index like this: $featured_image[index_of_alt_text] according to my understanding, the $featured_image array doesn't contain your alternative text or you didn't specify the index.

I hope it helped

| improve this answer | |
0
 if ( $projects->have_posts() ) : while ( $projects->have_posts() ) :
    $projects->the_post();

/*****     Thumbnail     ******/
    the_post_thumbnail(
        array(120, 90),     //Size of the thumbnail Image
        array(

            'class' => 'class_name',     //Specify class name to thumbnail image if any
            'alt' => 'post thumbnail',   // alternate text in case if no thumbnail image to show
            'title' => 'Notice'          //Specify title if want to
        )
    );
/*******     Thumbnail Ends   ********/
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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