无法在特色图像上显示或回显Alt文本

时间:2016-08-26 作者:clestcruz

我似乎在尝试显示特色图像的“可选文本”时遇到了问题。

这是我一直在研究的代码。不确定为什么备选文本显示为空白。

        <?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(); ?>

2 个回复
SO网友:Belal Mostafa Amin

我不太确定我要说什么,但快速看,$featured\\u image是一个数组(URL, width, height, is_intermediate). 因此,假设此数组中存在备选文本,则需要如下指定其索引:$featured_image[index_of_alt_text]据我了解,$featured\\u图像数组不包含您的可选文本,或者您没有指定索引。

我希望有帮助

SO网友:Rishabh
 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   ********/