0

我在我的网站的子文件夹中安装了一个 wordpress 博客页面(从 html 转换)和一个静态 html 主页。 我想在主页上显示 3 个最新帖子及其特色图片。使用下面的代码,我可以显示最新的帖子文本,但我不知道如何显示帖子的特色图片。 在 wordpress 自定义主题的 index.php 中,我将精选照片放在了一个 div 中:

<div id="blogphoto"><?php the_post_thumbnail(); ?></div>

这是提取最新帖子的静态 html index.php 页面上的代码。任何人都可以帮助我获得这些帖子的特色图片吗?

<div id="wp-post">

      <?php
      
      $args = array('numberposts'=>1);
      $recent_posts=wp_get_recent_posts($args);
      foreach( $recent_posts as $recent_post ){
      echo "<h3>".$recent_post['post_title']."</h3> <br>"; 
      echo "<span>".$recent_post['post_date']."</span> <br>";
      echo  "<p>".$recent_post['post_content']."</p><br><br>";
      
      }
      ?>
     </div>
    
    <div id="wp-post2">
    <?php
    
    $args = array('numberposts'=>1 , 'offset'=>1 );
    $recent_posts=wp_get_recent_posts($args);
    foreach( $recent_posts as $recent_post ){ 
    echo "<span>".$recent_post['post_title']."</span> <br>";
    echo  "<p>".$recent_post['post_content']."</p><br><br>";
    }

    ?>
    </div>

    <div id="wp-post3">
    <?php
    
    $args = array('numberposts'=>1 , 'offset'=>2 );
    $recent_posts=wp_get_recent_posts($args);
    foreach( $recent_posts as $recent_post ){ 
    echo "<span>".$recent_post['post_title']."</span> <br>";
    echo  "<p>".$recent_post['post_content']."</p><br><br>";
    }

    ?>
    </div>

4

1 回答 1

0

请尝试使用此代码the_post_thumbnail获取特征图像

<?php
if ( $query->have_posts() ) {
    $i = 1;
    while($query->have_posts()) { 
        echo '<div id="wp-post-'.$i.'">';
        $query->the_post();
        ?><h2><?php the_title(); ?></h2>
        <?php
        if ( has_post_thumbnail() ) { // only print out the thumbnail if it actually has one
            echo '<p>post says it has a featured image</p>'; // double checking
            the_post_thumbnail('thumbnail');
        } else {
            echo '<p>this post does not have a featured image</p>';
        }
        echo '</div>';
        $i++;
    }
} else {
    echo '<p>no posts found</p>';
}
?>
于 2018-05-06T09:48:54.863 回答