Replace extension on thumbnails

Viewed 53

I'm trying to do something in wordpress, by default I'm using webp images, i succeeded using the_content filter on post pages, but not on featured images on home pages.

This is the code i use on post pages;

<?php 

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

This is the code i use to show featured images on homepage

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

Replace with this;

<?php 

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

I tested it with the code you have.

Depends on your theme. this code works for mine:

add_filter('post_thumbnail_html', 'change_img');

For example, the following code hides post thumbnails image in single posts or pages, but keeps them on index page:

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