替换缩略图上的扩展名



我正在尝试在wordpress中做一些事情,默认情况下我使用的是webp图像,我成功地在帖子页面上使用了_content过滤器,但在主页上的特色图像上没有。

这是我在帖子页面上使用的代码;

<?php 
add_filter('the_content', 'change_img', 99);
function change_img( $content )
{
return str_replace('.webp', '.jpg', $content);
} ?>

这是我用来在主页上显示特色图像的代码

<ul>
<?php $ksf = new WP_Query( 'posts_per_page=8' ); ?>
<?php while ($ksf -> have_posts()) : $ksf -> the_post(); ?>
<li>
<a href="<?php the_permalink() ?>">

<figure>
<?php if( !empty(get_the_post_thumbnail()) ) {
$feat_image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), "full", true);

?>
<img  src="<?php echo (($feat_image[0]))?>" alt="<?php echo get_post_meta( get_post_thumbnail_id(), '_wp_attachment_image_alt', true ); ?>" width="750" height="422" />
<?php  } ?>
<figcaption>
<?php the_title(); ?>
</figcaption>
</figure>   </a>    

</li>

<?php endwhile;
wp_reset_postdata(); ?></ul>

替换为this;

<?php 
add_filter('wp_get_attachment_image_src', 'change_img', 99);
function change_img( $image )
{
return str_replace('.webp', '.jpg', $image);
} ?>

我用你的代码测试了它。

取决于您的主题。该代码适用于我的

add_filter('post_thumbnail_html', 'change_img');

例如,以下代码在单个帖子或页面中隐藏帖子缩略图图像,但将它们保留在索引页面上:

function hide_thumbnail($html) {
if (is_single()) {
return '';
} else {
return $html;
}
}
add_filter('post_thumbnail_html', 'hide_thumbnail');

最新更新