WordPress:在首页显示帖子图像

时间:2014-04-04 07:49:54

标签: php wordpress image

希望有人可以提供帮助。我已将此代码放入标准模板中,但没有显示图像,尽管事实上我有很多带图像的帖子。

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post();    

$images =& get_children( array (
    'post_parent' => $post->ID,
    'post_type' => 'attachment',
    'post_mime_type' => 'image'
));

if ( empty($images) ) {
    // no attachments here
} else {
    foreach ( $images as $attachment_id => $attachment ) {
        echo wp_get_attachment_image( $attachment_id, 'thumbnail' );
    }
}

endwhile; endif; ?>

感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

变化

echo wp_get_attachment_image( $attachment_id, 'thumbnail' );

echo wp_get_attachment_image( $attachment->ID, 'thumbnail' );

$images =& get_children( array (
    'post_parent' => $post->ID,
    'post_type' => 'attachment',
    'post_mime_type' => 'image'
));

$images =get_posts( array (
    'post_parent' => $post->ID,
    'post_type' => 'attachment',
    'post_mime_type' => 'image'
));

你应该使用get_posts()

答案 1 :(得分:0)

您可以这样做:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post();    

 $args = array(
   'post_type' => 'attachment',
   'numberposts' => -1,
  );

  $attachments = get_posts( $args );
     if ( $attachments ) {
        foreach ( $attachments as $attachment ) {
           echo wp_get_attachment_image( $attachment->ID, 'full' );
          }
     }

 endwhile; endif; ?>

此代码将获取所有&#39;大&#39;来自媒体库的图像并显示它们

希望这就是你要找的东西

相关问题