我在这里浏览并找到了一些例子,但它们似乎没有带来图像。这是我到目前为止所发现的。这至少显示了帖子的链接,但我想只需要显示两个帖子,我需要显示特色图片。
<!-<div class="recent_posts_home_container">
<h2>Recent Posts</h2>
<ul>
<?php
$recent_posts = wp_get_recent_posts();
foreach( $recent_posts as $recent ){
echo '<li><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' . $recent["post_title"].'</a> </li> ';
}
?>
</ul>
</div>
<!-- end recent posts div -->
答案 0 :(得分:1)
<div class="recent_posts_home_container">
<h2>Recent Posts</h2>
<ul>
<?php
$args = array( 'numberposts' => '2' );
$recent_posts = wp_get_recent_posts($args);
foreach( $recent_posts as $recent ){
echo '<li><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' . $recent["post_title"].'</a> </li> ';
echo get_the_post_thumbnail($recent["ID"], 'thumbnail');
}
?>
</ul>
</div>
提示:在此处提出问题之前,请先查看Wordpress Stackexchange和codex。
答案 1 :(得分:1)
这应该有效:
function the_gallery() {
$attachments = get_children( array('post_parent' => get_the_ID(), 'post_type' => 'attachment', 'post_mime_type' => 'image') );
foreach ( $attachments as $attachment_id => $attachment )
{
echo wp_get_attachment_image($attachment_id);
}
}
我还会与您分享一些参考链接,这些链接可以帮助您更好地理解这个问题:
https://wordpress.org/support/topic/how-to-display-all-the-attached-images-of-the-posts
http://wpengineer.com/1735/easier-better-solutions-to-get-pictures-on-your-posts/