2014-09-01 30 views
0

我已經使用要素圖像到自定義帖子。我已經寫了下面的代碼來使用該功能圖像到我的插件。顯示「注意:未定義的變量:」和「注意:試圖獲取非對象的屬性」

while($q->have_posts()) : $q->the_post(); 

    $newsbox_post_img_src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), '', false, ''); 

    $list .= '<li class="news-item"> 
    <table cellpadding="4"> 
     <tr> 
      <td> 
       <img src="'.$newsbox_post_img_src[0].'" width="100" class="img-circle" /> 
      </td> 
      <td>'.get_the_excerpt().'</td> 
     </tr> 
    </table> 
     </li>'; 

endwhile; 

卻是露出

未定義的變量...

試圖讓非對象的屬性....

通知。

Error images

請提出一個解決方案。

回答

1

在循環中不需要使用$post->ID。此外,您將需要實際檢查$newsbox_post_img_src有事返回,否則將返回一個

試圖讓非對象

錯誤的性質。

你的代碼應該基本上是這個樣子

while($q->have_posts()) : $q->the_post(); 
    $newsbox_post_img_src = wp_get_attachment_image_src(get_post_thumbnail_id(), '', false, ''); 

    $list = '<li class="news-item">'; 
    $list .= '<table cellpadding="4">'; 
    $list .= '<tr>'; 
    $list .= '<td>'; 
    if(!empty($newsbox_post_img_src)) { 
     $list .= '<img src="'.$newsbox_post_img_src[0].'" width="100" class="img-circle" />'; 
    }  
    $list .= '</td>'; 
    $list .= '<td>'.get_the_excerpt().'</td>'; 
    $list .= '</tr>'; 
    $list .= '</table>'; 
    $list .= '</li>'; 
    echo $list; 

endwhile; 
wp_reset_postdata(); 
+0

請你解釋爲什麼'$後> ID'不需要? @Pieter Goosen – Bir 2014-09-01 20:19:29

+1

在循環中時,默認情況下使用當前帖子ID。這就是爲什麼你不需要明確指定一個帖子ID – 2014-09-02 02:53:24

+0

我從WPSE看到你已經發布了這個問題。從那裏,我看到你在短代碼中使用它。在這種情況下,'echo $ list;'應該是'return $ list;'應該返回shortcode輸出,而不是echo'd – 2014-09-02 04:23:18

相關問題