最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

theme development - cannot echo the_post_thumbnails

programmeradmin5浏览0评论

I'm using the following PHP code to echo post thumbnails.

  <?php
  $args = array(
  'post_type' => 'post' ,
  'orderby' => 'date' ,
  'order' => 'DESC' ,
  'posts_per_page' => 1,
  'category'         => '2',
  'paged' => get_query_var('paged'),
  'post_parent' => $parent
  );
  // The Query
  $the_query = new WP_Query( $args );

  // The Loop
  if ( $the_query->have_posts() ) {
      while ( $the_query->have_posts() ) {
          $the_query->the_post();
          echo '' . the_post_thumbnail() . '';
      }
  } else {
      // no posts found
  }
  /* Restore original Post Data */
  wp_reset_postdata();
   ?>

But I'm having issue, since this wont work, but when I try to echo get_post_title() it works perfectly fine. I can't figure out where do I got this wrong.

How can I display/echo post thumbnails?

I'm using the following PHP code to echo post thumbnails.

  <?php
  $args = array(
  'post_type' => 'post' ,
  'orderby' => 'date' ,
  'order' => 'DESC' ,
  'posts_per_page' => 1,
  'category'         => '2',
  'paged' => get_query_var('paged'),
  'post_parent' => $parent
  );
  // The Query
  $the_query = new WP_Query( $args );

  // The Loop
  if ( $the_query->have_posts() ) {
      while ( $the_query->have_posts() ) {
          $the_query->the_post();
          echo '' . the_post_thumbnail() . '';
      }
  } else {
      // no posts found
  }
  /* Restore original Post Data */
  wp_reset_postdata();
   ?>

But I'm having issue, since this wont work, but when I try to echo get_post_title() it works perfectly fine. I can't figure out where do I got this wrong.

How can I display/echo post thumbnails?

Share Improve this question asked Jul 12, 2020 at 17:27 Yotam DahanYotam Dahan 1096 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 2

Not sure what you're trying to do, but the problem you're having with this specific bit of code is that you're using the wrong function. You want get_the_post_thumbnail() instead. The function the_post_thumbnail() echoes the result of get_the_post_thumbnail().

$args = array(
    'post_type' => 'post',
    'orderby' => 'date',
    'order' => 'DESC',
    'posts_per_page' => 1,
    'category' => '2',
    'paged' => get_query_var('paged'),
    'post_parent' => $parent
);
// The Query
$the_query = new WP_Query($args);

// The Loop
if ( $the_query->have_posts() ) {
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
        echo get_the_post_thumbnail();
    }
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
'posts_per_page' => 1,

Do you really only want a single post to be returned? And what is the $parent value?

Since you don't want anything but the post thumbnail, specify that you only need the post id(s). Try:

  $args = array(
  'post_type'      => 'post' ,
  'orderby'        => 'date' ,
  'order'          => 'DESC' ,
  'posts_per_page' => 1,
  'category'       => '2',
  'paged'          => get_query_var('paged'),
  'post_parent'    => $parent,
  'fields'         => 'ids'
  );
  // The Query
  $the_query = new WP_Query( $args );

  if ( ! empty ( $query->posts ) ) {

      $post_ids = $query->posts; // just the post IDs

      foreach ( $post_ids as $post_id )
          echo get_the_post_thumbnail( $post_id, 'post-thumbnail' );

  } else {
      echo 'No posts were found';
  }
  /* Restore original Post Data */
  wp_reset_postdata();

A good and relevant read here.

发布评论

评论列表(0)

  1. 暂无评论