首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不能回波the_post_thumbnails

不能回波the_post_thumbnails
EN

WordPress Development用户
提问于 2020-07-12 17:27:02
回答 2查看 28关注 0票数 0

我使用以下PHP代码来回显post缩略图。

代码语言:javascript
复制
   '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()时,它的工作非常好。我不知道我在哪里搞错了。

如何显示/回波后缩略图?

EN

回答 2

WordPress Development用户

发布于 2020-07-13 00:25:29

不确定您想要做什么,但是这段特定代码的问题是您使用了错误的函数。您需要的是get_the_post_thumbnail()。函数the_post_thumbnail()回显get_the_post_thumbnail()的结果。

代码语言:javascript
复制
$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();
票数 2
EN

WordPress Development用户

发布于 2020-07-12 18:32:25

代码语言:javascript
复制
'posts_per_page' => 1,

你真的只想要回一个帖子吗?$parent值是多少?

由于您只需要post缩略图,所以指定您只需要post id(s)。尝试:

代码语言:javascript
复制
  $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();

一个好的,相关的阅读这里

票数 0
EN
页面原文内容由WordPress Development提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://wordpress.stackexchange.com/questions/370916

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档