2016-12-29 85 views
1

我在wordpress中創建自定義主題,並顯示問題get_the_post_thumbnail();嘗試了幾種不同的方式,但沒有成功。它迴應'沒有縮略圖',即使有縮略圖Wordpress get_the_post_thumbnail();問題

<?php 
/* 
* Template Name: Blog Page 
*/ 
get_header(); ?> 

<div class="top-image"> 
<div class="headlines-2"> 
     <h1 class="big">blog</h1> 

     </div> 
     </div> 

<div id="primary" class="col-md-12"> 
    <main id="main" class="site-main" role="main"> 
<div class="news"> 
<?php    
$args = array(
'cat' => 9, 
    'posts_per_page' => '70' 
); 

// The Query 
$the_query = new WP_Query($args); 


// The Loop 
if (has_post_thumbnail($_post->ID, 'thumbnail')){ 
$photo = get_the_post_thumbnail($_post->ID, 'thumbnail'); 
echo '<div class="section1-2singlephoto">' . $photo . '</div>'; 
} 

else { 
echo 'no Thumbnail'; 
} 
if ($the_query->have_posts()) { 
while ($the_query->have_posts()) { 
    $the_query->the_post(); 
echo '<div class="boxed col-md-4">'; 
echo '<h2 class="titles">' . get_the_title() . '</h2>'; 
echo '<span class="rule"></span>'; 
    echo '<div class="author-info">'. get_the_date() .'<br>' . 
    get_the_author_link() .'</div>'; 
    echo get_template_part('template-parts/content', get_post_format() 
); 

      echo "</div>"; 

     } 
     } else { 
// no posts found 
} 
/* Restore original Post Data */ 
wp_reset_postdata();?> 
</div> 


    </main><!-- #main --> 
</div><!-- #primary --> 

<?php 

get_footer(); 
+1

格式代碼 –

回答

0

試試吧。

$ post_thumbnail_id = get_post_thumbnail_id($ _ post-> ID); $ post_thumbnail_url = wp_get_attachment_url($ post_thumbnail_id);

「/>

1

您正在檢查您的自定義帖子循環之外的縮略圖,所以我假設您期待將縮略圖應用於頁面本身而不是單個帖子。

您遇到的第一個問題是$_post未定義,因此has_post_thumbnail()將始終評估爲false。

使用$post代替,但首先要包含global $post;

其次,另一個用戶已經指出,has_post_thumbnail不接受多個參數(儘管這不太可能導致問題)。

示例1 - 顯示頁面的縮略圖:

改變這一點 -

if (has_post_thumbnail($_post->ID, 'thumbnail')){ 
    $photo = get_the_post_thumbnail($_post->ID, 'thumbnail'); 
    echo '<div class="section1-2singlephoto">' . $photo . '</div>'; 
} 

else { 
    echo 'no Thumbnail'; 
} 

爲了這一點 -

global $post; 

if (has_post_thumbnail($post)) { 
    $photo = get_the_post_thumbnail($post->ID, 'thumbnail'); 
    echo '<div class="section1-2singlephoto">' . $photo . '</div>'; 
} else { 
    echo 'no Thumbnail'; 
} 

例2 - 顯示個別文章縮略圖:

將以下您的文章循環 -

if (has_post_thumbnail()) { // note the removal of all args. 

    // here we switch from using an ID to null 
    $photo = get_the_post_thumbnail(null, 'thumbnail'); 
    echo '<div class="section1-2singlephoto">' . $photo . '</div>'; 
} else { 
    echo 'no Thumbnail'; 
}