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

如何在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/

    已采纳该答案
    评论
    解决 无用
    打赏 举报