wp_get_attachment_image没有工作wordpress来显示博客帖子图片

时间:2016-08-05 13:09:56

标签: php wordpress

我正在使用wordpress并尝试创建博客登录页面以显示最新的博客帖子。到目前为止这么好,但我在图像标签中显示博客图片时遇到了困难。我可以使用get_the_id函数获取postId。我也可以使用the_date函数获取帖子的日期。

但是,我无法使用wp_get_attachment_image功能来显示博客文章的图像。

请参阅下面的代码。

<?php $query = new WP_Query( 'posts_per_page=5' ); ?> 

<?php while ($query -> have_posts()) : $query -> the_post(); ?>
    <div class="blog">
      <img src="wp_get_attachment_image( get_the_ID() ); ">
      <h3><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
      <p><?php the_date(); ?></p>
      <p><?php the_excerpt(__('(more…)')); ?></p>
    </div>

    <?php
        endwhile;
        wp_reset_postdata();
    ?>

3 个答案:

答案 0 :(得分:0)

使用以下代码获取附加图像。

Component

答案 1 :(得分:0)

函数wp_get_attachment_image正在等待attachement_id

请参阅下面的wp,我们如何从帖子中获取所有附件:

<?php if ( $post->post_type == 'data-design' && $post->post_status == 'publish' ) {
        $attachments = get_posts( array(
            'post_type' => 'attachment',
            'posts_per_page' => -1,
            'post_parent' => $post->ID,
            'exclude'     => get_post_thumbnail_id()
        ) );

        if ( $attachments ) {
            foreach ( $attachments as $attachment ) {
                $class = "post-attachment mime-" . sanitize_title( $attachment->post_mime_type );
                $thumbimg = wp_get_attachment_link( $attachment->ID, 'thumbnail-size', true );
                echo '<li class="' . $class . ' data-design-thumbnail">' . $thumbimg . '</li>';
            }

        }
    }
?>

答案 2 :(得分:0)

我必须使用以下函数the_post_thumbnail()并在图像标记中回显结果

<img src="<?php echo the_post_thumbnail();?>">

以下代码有效。

<?php $query = new WP_Query( 'posts_per_page=5' ); ?> 

<?php while ($query -> have_posts()) : $query -> the_post(); ?>
    <div class="blog">
      <img src="wp_get_attachment_image( get_the_ID() ); ">
      <h3><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
      <p><?php the_date(); ?></p>
      <p><?php the_excerpt(__('(more…)')); ?></p>
    </div>
?>
相关问题