0

我在这里浏览并找到了一些示例,但它们似乎没有将图像带入。这是​​我到目前为止发现的内容。这至少显示了帖子的链接,但我只需要显示两个帖子,并且我需要显示特色图片。

<!-<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 -->
4

2 回答 2

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 Stackexchangecodex

于 2013-10-17T12:01:16.407 回答
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/

于 2015-01-15T10:08:18.037 回答