dongzongzi0379 2015-05-26 22:21
浏览 63
已采纳

如何制作响应式缩略图并在WordPress上应用后备图像?

I already have the two separate codes, both working, but I do not know how to use them in the same thumbnail.

This code set a default fallback image if no image can be found in a post.

<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>

And this code turn the thumbnail responsive.

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

I don't know PHP. I would like to remove this line below from the first code to resize the image to width 100 %. I don't want the fixed size of 600 x 600px.

the_post_thumbnail( array(600,600) );

Somebody can help me?

[ SOLUTION!! ] Thank you, 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>
  • 写回答

1条回答 默认 最新

  • drvxnivoqf17568697 2015-05-26 23:00
    关注

    try this. untested

    <?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 
    
    } ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100