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

报告相同问题?

悬赏问题

  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?