无法回显_POST_缩略图

时间:2020-07-12 作者:Yotam Dahan

我正在使用下面的PHP代码来回显帖子缩略图。

  <?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();
   ?>
但我有问题,因为这行不通,但当我尝试回应时get_post_title() 它工作得非常好。我不知道我哪里弄错了。

如何显示/回显帖子缩略图?

2 个回复
SO网友:Steve Johnson

不确定您想做什么,但这段特定代码的问题是您使用了错误的函数。你想要的get_the_post_thumbnail() 相反函数The\\u post\\u thumbnail()回显get\\u The\\u post\\u 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();

SO网友:shanebp

\'posts_per_page\' => 1,
你真的只想退回一个帖子吗?什么是$parent 价值

由于您只需要帖子缩略图,请指定只需要帖子id。尝试:

  $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();
良好且相关的阅读here.

相关推荐