PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, February 2, 2022

[FIXED] Replace extension on thumbnails

 February 02, 2022     php, str-replace, wordpress     No comments   

Issue

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>

Solution

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.



Answered By - awakening
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing