• Hi! I need to replace the default thumbnail output html: <img src=”image.png” alt=”” …/> with <amp-img src=”image.png” ….> nothing more. Please help me. I want to develop my theme for Google Mobile AMP. thanks

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator Jose Castaneda

    (@jcastaneda)

    THEME COFFEE MONKEY

    Hi there!

    Not sure which thumbnail you are looking to modify but have you looked into what image functions WordPress does have? One that comes to mind is:
    https://developer.www.ads-software.com/reference/functions/wp_get_attachment_image_src/

    If you are looking for just the post thumbnail you can try using the post_thumbnail_html filter:
    https://developer.www.ads-software.com/reference/hooks/post_thumbnail_html/

    Hope that helps you out a bit!

    animaleja32

    (@animaleja32)

    I have the same question as parazi7u…

    I got this snippet… I think I’m close, but not quite there yet. Any idea on how to convert into <amp-img>?

    function isa_amp_featured_img( $size = ‘medium’ ) {

    global $post;

    if ( has_post_thumbnail( $post->ID ) ) {

    $thumb_id = get_post_thumbnail_id( $post->ID );
    $img = wp_get_attachment_image_src( $thumb_id, $size );
    $img_src = $img[0];
    $w = $img[1];
    $h = $img[2];

    $alt = get_post_meta($post->ID, ‘_wp_attachment_image_alt’, true);

    if(empty($alt)) {
    $attachment = get_post( $thumb_id );
    $alt = trim(strip_tags( $attachment->post_title ) );
    } ?>
    <amp-img id=”feat-img” src=”<?php echo esc_url( $img_src ); ?>” <?php
    if ( $img_srcset = wp_get_attachment_image_srcset( $thumb_id, $size ) ) {
    ?> srcset=”<?php echo esc_attr( $img_srcset ); ?>” <?php
    }
    ?> alt=”<?php echo esc_attr( $alt ); ?>” width=”<?php echo $w; ?>” height=”<?php echo $h; ?>”>
    </amp-img>
    <?php
    }
    }

    function my_amp_set_custom_template( $file, $type, $post ) {
    if ( ‘single’ === $type ) {
    $file = get_stylesheet_directory() . ‘/amp/single.php’;
    }
    return $file;
    }
    add_filter( ‘amp_post_template_file’, ‘my_amp_set_custom_template’, 10, 3 );

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Modify thumbnail output html img src with AMP-img src’ is closed to new replies.