2015-06-24 22 views
1

基本上圖像標籤,我想創建的格式爲:PHP的WordPress的get_the_post_thumbnail - 數組的問題(可能是很容易解決)

<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,因爲我在上面的代碼鏈接中看到了它,並認爲它會起作用。大聲笑。

+0

不知道是否可以使用「shop_catalog」作爲get_the_post_thumbnail()函數的第二個參數。認爲它必須是預定義數值(縮略圖,中等,大或全)數組或指定大小的數組之一。 –

+0

我認爲我的主題增加了另一個價值。當我開始定製時,它已經被使用 –

回答

0

我asuming您已經註冊的「shop_catalog」使用add_image_size

<?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.'">'; 
    } 
?> 
相關問題