2017-05-15 36 views
0

下面的代码放在一个包含文件中名为最新-web.php:get_the_post_thumbnail()在页面模板不工作

<?php 
        $args = array('numberposts' => '8', 'category_name' => 'web-reference'); 
        $recent_posts = wp_get_recent_posts($args); 
        foreach($recent_posts as $recent){ 
         $featured_image = get_the_post_thumbnail(); 
         $poveznica = get_field('link-projekta'); 
         echo '<figure class="effect-winston"> 
         ' . $featured_image . ' 
         <figcaption> 
          <h2><a href="' . get_permalink($recent["ID"]) . '">' . $recent["post_title"] . '</a></h2> 
          <p> 
           <a href="' . get_permalink($recent["ID"]) . '"><i class="fa fa-fw fa-list"></i></a> 
           <a href="' . $poveznica . '" target="_blank"><i class="fa fa-fw fa-link"></i></a> 
          </p> 
         </figcaption>   
        </figure>'; 
        } 
        wp_reset_query(); 
?> 

当包括的index.html文件,它完美的作品,但是当包括它在页面模板中,有一个问题。该代码正确地提取所有信息。但是,图像/缩略图完全不显示。从浏览器检查时没有img标签。有没有人有这个解决方案?

谢谢!

+0

你试过 $ featured_image = get_the_post_thumbnail($ recent [“ID”]); – Ashkar

+0

你好,我刚刚尝试过。在使用您的代码时,它在索引和页面模板上都不起作用。 – Rockbell

回答

0

请尝试此操作,在缩略图功能中添加了最近的ID。

<?php 
        $args = array('numberposts' => '8'); 
        $recent_posts = wp_get_recent_posts($args); 
        foreach($recent_posts as $recent){ 
         $featured_image = get_the_post_thumbnail($recent["ID"]); 
         $poveznica = get_field('link-projekta'); 
         echo '<figure class="effect-winston"> 
         ' . $featured_image . ' 
         <figcaption> 
          <h2><a href="' . get_permalink($recent["ID"]) . '">' . $recent["post_title"] . '</a></h2> 
          <p> 
           <a href="' . get_permalink($recent["ID"]) . '"><i class="fa fa-fw fa-list"></i></a> 
           <a href="' . $poveznica . '" target="_blank"><i class="fa fa-fw fa-link"></i></a> 
          </p> 
         </figcaption>   
        </figure>'; 
        } 
        wp_reset_query(); 
?> 
+0

第一次尝试时我肯定犯了错误。完美的作品。非常感谢你! – Rockbell

0

在一段时间get_the_post_thumbnail()不工作,直到你在你的情况foeach环通后的ID,以便使哟必须通过最近的[ID]在get_the_post_thumbnail(), 所以它看起来像get_the_post_thumbnail (最近['ID']),你做伴侣