• 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>
Viewing 1 replies (of 1 total)
  • 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) !='' )

Viewing 1 replies (of 1 total)
  • The topic ‘Showing Posts in Menu’ is closed to new replies.