duanpa1898 2017-04-10 13:03
浏览 33
已采纳

如何在WordPress中悬停链接时如何显示帖子的图片

There is a part in my website where the last 6 posts are reviewed and this is the HTML code for it:

<div class="latest-posts">
      <div id="latest-posts-title">
    <p>My Website's latest posts</p>
  </div>
      <div id="latest-posts-pictures"> <img src=""width="190px" height="190px" alt="latest-posts-pictures"> </div>
      <ul>
    <li><a href="#">Post1</a></li>
    <li><a href="#">Post2</a></li>
    <li><a href="#">Post3</a></li>
    <li><a href="#">Post4</a></li>
    <li><a href="#">Post5</a></li>
    <li><a href="#">Post6</a></li>
  </ul>

I want to add a feature to make the user able to see the post's picture when he/she hovers the post's link in the code above.

I've used this PHP code and it works well:

<ul>
<?php
$my_query = new WP_Query('showposts=6&cat=my_category');
while ($my_query->have_posts()):
$my_query->the_post();
$do_not_duplicate = $post->ID;?>

<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>

<?php endwhile; ?>
</ul>

But this code does not contain the picture part that I want. Do you know how can I make this feature available for my users?

Please note that it is not important for me to use PHP or JavaScript, all I want is to make it possible for the user to see the post's picture when he hovers its link.

  • 写回答

1条回答 默认 最新

  • dps43378 2017-04-10 13:36
    关注

    You can display featured image of the post using following code:

    // Check if the post has a Post Thumbnail assigned to it.
    if ( has_post_thumbnail() ) {
        the_post_thumbnail();
    }
    

    You can check this link for more information. https://developer.wordpress.org/reference/functions/the_post_thumbnail/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启