缺少最新发布的缩略图替代文本

时间:2016-11-10 作者:QJD

我在WordPress自定义主题主页上使用以下代码来显示最近的三篇文章:

<?php query_posts(\'post_type=portfolio&showposts=3\'); ?>
        <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

            <div class="col-lg-4 col-md-4 col-sm-12 col-xs-12" <?php post_class() ?> id="post-<?php the_ID(); ?>">
                <a href="<?php the_permalink() ?>" class="portfolio-box">

                    <?php if ( has_post_thumbnail() ) {
                            $image_src = wp_get_attachment_image_src( get_post_thumbnail_id(),’thumbnail’ );
                             echo \'<img width="100%" src="\'.$image_src[0].\'" alt="\'.$image_alt.\'" title="\'.$image_title.\'">\';
                        } else { ?>
                        <img src="<?php bloginfo(\'template_directory\'); ?>/img/fallback.jpg" class="img-responsive" />
                    <?php } ?>

                   <div class="portfolio-box-caption portfolio-box-caption-blue">
                        <div class="portfolio-box-caption-content">
                            <div class="project-category text-faded">
                                <!-- <?php the_category( \', \' ); ?> -->
                            </div>
                            <div class="project-name-work">
                                <?php the_title(); ?>
                            </div>
                        </div>
                    </div>
                </a>
            </div>

        <?php endwhile; endif; ?>
但是,图像alt文本不会出现在最终页面源中。有人能告诉我哪里出了问题吗?

谢谢

2 个回复
SO网友:Benoti

您需要定义$image_alt$image_title. 我看不出你在代码中做了什么。

您需要这样更改/添加

<?php if ( has_post_thumbnail() ) {
      $image_src = wp_get_attachment_image_src( get_post_thumbnail_id(),’thumbnail’ );
      $attachment_meta = get_post(get_post_thumbnail_id());
      echo \'<img width="100%" src="\'.$image_src[0].\'" alt="\'.$attachment_meta->post_excerpt.\'" title="\'.$attachment_meta->post_title.\'">\';
      } else { ?>
        <img src="<?php bloginfo(\'template_directory\'); ?>/img/fallback.jpg" class="img-responsive" />
    <?php } ?>
图像的“alt”文本以字符串形式存储在wp\\u postmeta中,meta\\u键为“\\u wp\\u attachment\\u Image\\u alt”。

你可以这样抓

$img_alt =  get_post_meta($thumb_id, \'_wp_attachment_image_alt\', true);

EDIT:

<?php if ( has_post_thumbnail() ) {
      $image_src = wp_get_attachment_image_src( get_post_thumbnail_id(),\'thumbnail\' );
      $attachment_meta = get_post(get_post_thumbnail_id());
      $img_alt =  get_post_meta(get_post_thumbnail_id(), \'_wp_attachment_image_alt\', true);
      echo \'<img width="100%" src="\'.$image_src[0].\'" alt="\'.$img_alt.\'" title="\'.$attachment_meta->post_title.\'">\';
      } else { ?>
        <img src="<?php bloginfo(\'template_directory\'); ?>/img/fallback.jpg" class="img-responsive" />
    <?php } ?>

SO网友:QJD

<?php if ( has_post_thumbnail() ) { $image_src = wp_get_attachment_image_src( get_post_thumbnail_id(),’thumbnail’ ); $img_alt = get_post_meta(get_post_thumbnail_id(), \'_wp_attachment_image_alt\', true); $attachment_meta = get_post(get_post_thumbnail_id()); echo \'<img width="100%" src="\'.$image_src[0].\'" alt="\'.$img_alt.\'" title="\'.$attachment_meta->post_title.\'">\'; } else { ?> <img src="<?php bloginfo(\'template_directory\'); ?>/img/fallback.jpg" class="img-responsive" /> <?php } ?>