2017-04-10 13:03
浏览 29


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 id="latest-posts-pictures"> <img src=""width="190px" height="190px" alt="latest-posts-pictures"> </div>
    <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>

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:

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

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

<?php endwhile; ?>

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() ) {

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

    解决 无用
    打赏 举报