dongyi1111 2016-08-05 13:09
浏览 27
已采纳

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

I am using wordpress and trying to create a blog landing page to show the most recent blog posts. So far so good but I am having difficulties showing the blog image in the image tag. I am able to obtain the postId by using get_the_id function. I was also able to get the date of the post by using the_date function.

However, I cannot get the wp_get_attachment_image function to show the image of the blog post.

Please see my code below.

<?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条回答 默认 最新

  • doubao12345 2016-08-05 14:05
    关注

    I had to use the following function the_post_thumbnail() and echo the result within an image tag

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

    The following code worked.

    <?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>
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘