【问题标题】:How to make a responsive thumbnail and apply fallback image on WordPress?如何在 WordPress 上制作响应式缩略图并应用后备图像?
【发布时间】:2015-08-08 19:12:48
【问题描述】:

我已经有两个单独的代码,都可以工作,但我不知道如何在同一个缩略图中使用它们。

如果在帖子中找不到图片,此代码会设置默认的后备图片。

<a href="<?php the_permalink(); ?>">
<?php if ( has_post_thumbnail() )
the_post_thumbnail( array(600,600) );
else
echo '<img src="' . trailingslashit( get_stylesheet_directory_uri() ) . 'images/default-thumbnail.svg' . '" />';
?>
</a>

并且此代码使缩略图响应。

<? if( has_post_thumbnail( $post_id ) ): ?>
<img title="" alt="" src="<?=wp_get_attachment_url( get_post_thumbnail_id() ); ?>" style="width:100%; height:auto;">
<? endif; ?>

我不懂 PHP。我想从第一个代码中删除下面的这一行,以将图像大小调整为 100%。我不想要 600 x 600px 的固定尺寸。

the_post_thumbnail( array(600,600) );

有人可以帮助我吗?

[解决方案!! ] 谢谢你,Society43。

        <a href="<?php the_permalink(); ?>">
          <?php if( has_post_thumbnail( $post_id ) ) { ?>
          <img src="<?=wp_get_attachment_url( get_post_thumbnail_id() ); ?>" style="width:100%; height:auto;">
          <?php } else {
          echo '<img src="' . trailingslashit( get_stylesheet_directory_uri() ) . 'images/default-thumbnail.svg' . '" />';
          } ?>
        </a>

【问题讨论】:

    标签: php wordpress thumbnails fallback


    【解决方案1】:

    试试这个。未经测试

    <?php if( has_post_thumbnail( $post_id ) ) { ?>
    <img title="" alt="" src="<?=wp_get_attachment_url( get_post_thumbnail_id() ); ?>" style="width:100%; height:auto;">
    
    <?php } else { 
    
    //fallback img here 
    
    } ?>
    

    【讨论】:

      猜你喜欢
      • 2012-01-26
      • 1970-01-01
      • 2014-04-15
      • 2017-03-07
      • 1970-01-01
      • 2015-05-20
      • 2021-08-20
      • 2018-07-04
      • 1970-01-01
      相关资源
      最近更新 更多