如何制作响应式缩略图并在 WordPress 上应用后备图像?
How to make a responsive thumbnail and apply fallback image on WordPress?
我已经有两个单独的代码,都可以使用,但我不知道如何在同一个缩略图中使用它们。
如果在 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>
并且此代码使缩略图响应。
<? 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 600 像素的固定尺寸。
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 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
} ?>
我已经有两个单独的代码,都可以使用,但我不知道如何在同一个缩略图中使用它们。
如果在 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>
并且此代码使缩略图响应。
<? 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 600 像素的固定尺寸。
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 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
} ?>