【问题标题】:the_post_thumbnail in WordPress Loop wont workWordPress Loop 中的 the_post_thumbnail 不起作用
【发布时间】:2018-06-10 11:05:50
【问题描述】:

我也是新手,我无法让我的循环显示来自 WordPress 帖子的特色图片。

我已尝试使用 the_post_thumbnail 和 查看https://codex.wordpress.org/Post_Thumbnails 和其他类似问题。

希望你能帮忙。

我的循环现在看起来像这样:

  <?php
  $query = new WP_Query(array(
      'posts_per_page'   => 4,
  ));

  while ($query->have_posts()): $query->the_post(); ?>
     <ul>
         <li><?php the_title(); ?></li>
         <li><?php echo get_the_date(); ?></li>
         <li><?php
            if ( has_post_thumbnail() ) {
                the_post_thumbnail('thumbnail');
            } ?>
         </li>
     </ul>
  <?php endwhile;
  wp_reset_postdata();
  ?>

【问题讨论】:

  • 是否正确显示了标题和日期?
  • 嗨 Sofie,您能否确认此循环是用于自定义帖子类型还是默认 WordPress 帖子?
  • 感谢@Jagir bahesh,我已经解决了这个问题。 (看最后一个答案)

标签: php wordpress loops templatetags


【解决方案1】:

这里我给你发送代码,请检查。

<?php

//Args
$myquery = array(
    'post_type'      => 'post', // Here you add your post type
    'posts_per_page' => 4,
    'orderby'        => 'post_date', 
    'order'          => 'DESC'     
);

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
    $the_query->the_post(); ?>
    <?php /* grab the url for the full size featured image */
    $featured_img_url = get_the_post_thumbnail_url(get_the_ID(),'full'); ?>
    <ul>
        <li><?php echo get_the_title(); ?></li>
        <li><?php echo get_the_date(); ?></li>
        <li><img src="<?php echo $featured_img_url; ?>" /></li>
    </ul>
<?php }
/* Restore original Post Data */
wp_reset_postdata();
} else {
// no posts found
}

【讨论】:

  • 确保您在帖子/页面中添加了特色图片。
  • 嗨。谢谢,这是我缺少的“特色图片”。我刚刚在帖子文本本身中添加了图片,而不是特色图片框。
  • 不应该 $the_query = new WP_Query( $args );$the_query = new WP_Query( $myquery );
【解决方案2】:

你可以试试这个代码,也许它可以工作。

<?php
  $query = new WP_Query(array(
      'posts_per_page'   => 4,
  ));

  while ($query->have_posts()): $query->the_post(); ?>
     <ul>
         <li><?php the_title(); ?></li>
         <li><?php echo get_the_date(); ?></li>
         <li><?php
            if ( has_post_thumbnail() ) {
                $image = get_the_post_thumbnail('thumbnail');
            } ?>
         </li>
     </ul>
  <?php endwhile;
  wp_reset_postdata();
  ?>

【讨论】:

    【解决方案3】:

    您需要将add_theme_support( 'post-thumbnails' ); 添加到您的functions.php 文件中才能使用缩略图

    【讨论】:

      【解决方案4】:

      我已经修复了代码。在这种情况下,您可以使用get_the_post_thumbnail( $post_id ) 函数并将其分配给变量。然后,你可以echo它。

      <?php
        $query = new WP_Query(array(
            'posts_per_page'   => 4,
        ));
      
        while ($query->have_posts()): $query->the_post(); ?>
           <ul>
               <li><?php the_title(); ?></li>
               <li><?php echo get_the_date(); ?></li>
               <li><?php
                  $thumbnail = '';
                  if ( has_post_thumbnail( get_the_ID() ) ) {
                      $thumbnail = the_post_thumbnail( get_the_ID(), 'thumbnail');
                  }
                  echo $thumbnail; ?>
               </li>
           </ul>
        <?php endwhile;
           wp_reset_postdata();
        ?>
      

      您可以在How to get featured image in WordPress - the_post_thumbnail & get_the_post_thumbnail了解这两个功能之间的详细区别

      【讨论】:

      • 建议的编辑队列已满,所以我在这里做一个说明:您自相矛盾,$thumbnail = the_post_thumbnail(...) 的用法不正确。应该是$thumbnail = get_the_post_thumbnail(...)
      猜你喜欢
      • 1970-01-01
      • 2018-10-18
      • 2018-01-05
      • 1970-01-01
      • 2013-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多