php wordpress get_the_post_thumbnail-数组问题(可能很容易修复)


php wordpress get_the_post_thumbnail - array issue (probably easy fix)

基本上,我试图创建的图像标签的格式是:

<img src="data:image/png;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs=" class="attachment-catalog" data-src="my/image/path/here">

这是我的代码:

<?php
    if ( has_post_thumbnail( $post->ID ) ) {
        $newimgdetails = array(
                            'src'      => "data:image/png;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs=",
                            'class'    => "attachment-$size",
                            'alt'      => trim( strip_tags( $attachment->post_excerpt ) ),
                            'title'    => trim( strip_tags( $attachment->post_title ) ),
                            'data-src' => $src,
                          );    
        echo  get_the_post_thumbnail( $post->ID, 'shop_catalog', $newimgdetails);
    }
?>

我是php的新手,所以我认为这是一个简单的修复方法,但在过去的一个小时里,我一直在处理这个问题。

有人能帮我弄清楚这个吗?

以下是get_the_post_thumbnail的代码集链接-https://codex.wordpress.org/Function_Reference/get_the_post_thumbnail

*更新-用谷歌的图片试用了这个代码,它很有效。问题是我的变量$src。我需要这个来获得图像的路径,而它目前还没有这样做。我使用$src是因为我在上面的codex链接中看到了它,并认为它会起作用。lol.

我很高兴您已经使用add_image_size 注册了"shop_catalog"

<?php
    if ( has_post_thumbnail( $post->ID ) ) {
        $thumb_id = get_post_thumbnail_id( $post->ID );
        $thumb_url_array = wp_get_attachment_image_src($thumb_id, 'shop_catalog', true);
        $src = $thumb_url_array[0];
        echo '<img scr="data:image/png;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs=" class="attachment-catalog" alt="'.trim( strip_tags( $attachment->post_excerpt ) ).'" title="'.trim( strip_tags( $attachment->post_title ) ).'" data-src="'.$src.'">';
    }
?>