php WordPress循环中的_post_thumbnail不起作用

tyg4sfes  于 2022-10-30  发布在  PHP
关注(0)|答案(5)|浏览(164)

我也是新来的,我不能让我的循环显示WordPress中帖子的特色图像。
我已经尝试了_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();
  ?>
huwehgph

huwehgph1#

这里我给你发的代码请查收。

<?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
}
e37o9pze

e37o9pze2#

你可以试试这个代码,也许它能用。

<?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();
  ?>
bsxbgnwa

bsxbgnwa3#

您需要将add_theme_support( 'post-thumbnails' );添加到您的functions.php文件中,以便缩略图正常工作

ugmeyewa

ugmeyewa4#

我已经修正了代码。在这种情况下,你可以使用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中了解这两个函数之间的详细差异

0sgqnhkj

0sgqnhkj5#

如果传回null。

<?php
    $featured_image = get_the_post_thumbnail(get_the_ID(),'full');

     if ($featured_image != null): ?>
          <?php echo $featured_image; ?>                    
      <?php else: ?>
          <img src="/wp-content/themes/sample/assets/images/default.png">
      <?php endif; ?>

相关问题