简体   繁体   English

wordpress get_the_post_thumbnail()不显示任何内容

[英]wordpress get_the_post_thumbnail() doesn't show anything

I'm creating my first wordpress theme, I can't figure out why the post thumbnails aren't showing. 我正在创建我的第一个wordpress主题,我不知道为什么未显示帖子缩略图。 it just does nothing(no errors). 它什么都不做(没有错误)。 Here is my code: 这是我的代码:

<?php
    $args = array( 'posts_per_page' => 3, 'category' => 6);
    $postslist = get_posts( $args );
    foreach ( $postslist as $post ) :
      setup_postdata( $post );
    ?>
      <div class="col-xs-12 col-sm-4">
        <h4><?php the_title(); ?></h4>
        <?php get_the_post_thumbnail('small'); ?>
        <p><?php the_excerpt(); ?></p>
      </div>      

    <?php
    endforeach; 
    wp_reset_postdata();
  ?>

I'm using HTML5Blank Theme. 我正在使用HTML5Blank主题。 And it supports thumbnails. 它支持缩略图。 this is the code for it in my functions.php file: 这是我的functions.php文件中的代码:

add_theme_support('post-thumbnails');
add_image_size('large', 700, '', true);
add_image_size('medium', 250, '', true);
add_image_size('small', 120, '', true);
add_image_size('custom-size', 700, 200, true);

我认为这是一个小的输入错误:samll-> small

You need to echo it like this echo get_the_post_thumbnail('small'); 您需要像这样echo get_the_post_thumbnail('small');它: echo get_the_post_thumbnail('small');
get_ functions store the data, they don't actually return it which is why you have to echo. get_函数存储数据,但实际上并没有返回数据,这就是您必须回显的原因。 They are useful in many cases like you could store it in a variable like $thumb-small = get_the_post_thumbnail('small'); 它们在许多情况下很有用,例如您可以将其存储在$thumb-small = get_the_post_thumbnail('small');这样的变量中$thumb-small = get_the_post_thumbnail('small'); and reuse it throughout the page. 并在整个页面中重复使用。

the the_post_thumbnail function also used for get post image you can also do by this way. the_post_thumbnail函数也用于获取帖子图像,您也可以通过这种方式进行操作。

<?php 
 if ( has_post_thumbnail() ) {
       the_post_thumbnail('small');
 }

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM