我在我的网站的子文件夹(从html转换)和静态html主页中安装了wordpress博客页面。 我想在主页上显示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>
答案 0 :(得分: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>';
}
?>