WordPress.org

Ready to get started?Download WordPress

Forums

Showing Posts in Menu (2 posts)

  1. Vajrasar Goswami
    Member
    Posted 1 year ago #

    I wanted to show my recent 2 posts in main menu so I did few lines of code and achieved showing titles under the category name on hover, and also tried to fetch the first image of the post. The problem is, only the first-post in the list shows up its first-image, not other posts.

    I am including the screen shot as it is on my localhost. -> ScreenShot

    In the screenshot am hovering over "around Town" category to show two posts from it. In first post it is showing the first-image of the post; but not doing so in the second post. This is my question, why?

    This is what I used Below-Header in simple hook ->

    <ul id="nav2">
    	<li>
    		Around Town
    		<ul>
    			<li>
    <?php
    $featuredPosts = new WP_Query();
    $featuredPosts->query('showposts=2&cat=9');
    while ($featuredPosts->have_posts()) : $featuredPosts->the_post(); ?>
    <?php if ( get_the_post_thumbnail($post_id) !='' )
    {
       the_post_thumbnail(array(30,30));
    } ?>
    <h1><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h1>
    <?php endwhile; ?>
    </li>
    		</ul>
    	</li>
    
    </ul>
  2. alchymyth
    Forum Moderator
    Posted 1 year ago #

    try and simplify this:

    <?php if ( get_the_post_thumbnail($post_id) !='' )
    {
       the_post_thumbnail(array(30,30));
    } ?>

    to:

    <?php  the_post_thumbnail(array(30,30)); ?>

    there is no need to check for the thumbnail, as the code would not output anything if there is no thumbnail;
    also, I would think the check would have needed to be:

    if ( get_the_post_thumbnail($post->ID) !='' )

Topic Closed

This topic has been closed to new replies.

About this Topic