Posts from multiblt categories but featured image only in the first one, the rest to have the post title only

General notes

  1. 1st of all: You don’t have to use the (imho crappy) TimThumb thingy. There’re much better things like my (freely available, open source) »Dynamic image resize« plugin.
  2. 2nd (and afaik), you can do this with on board stuff, like get_image_tag() in your case.
  3. 3rd: Don’t throw that many variables into the global namespace. If you really think you can’t work around it: prefix them with a unique string like wpse69602_.
  4. 4th Try to write clean code with lots of white space. This helps you spotting errors like this: 'include' => '4,5,6,7'. Hint: Most of those functions take arrays and not special-character separated strings.

Your cleaned up code

The kool thing I used to identify the number of the currently looped post isn’t a $flag. It’s a part of the $wp_query object: The current_post, which is a flag already set by WordPress.

<div class="container">
<?php
$terms = get_terms(
     'category'
    ,array(
         'include' => array( '4,5,6,7' )
        ,'orderby' => 'name'
        ,'order'   => 'ASC'
    )
);
if ( ! empty( $terms ) )
{
    foreach( $terms as $term ) 
    {
        $my_query = new WP_Query( array(
             'category__in'     => array( $term->term_id )
            ,'post_type'        => 'post'
            ,'post_status'      => 'publish'
            ,'posts_per_page'   => 1
            ,'caller_get_posts' => 1
        ) );
        if ( $my_query->have_posts() )
        {
            while ( $my_query->have_posts() )
            {
                $my_query->the_post();
                ?>

                <div class="post_home_main">

                <!-- Show the posts featured image -->
                <?php
                if ( 1 <= $my_query->current_post )
                {
                    if ( $image_url = wp_get_attachment_url( get_post_thumbnail_id() ) )
                    {
                        echo get_image_tag(
                             get_post_thumbnail_id()
                            ,__( 'Featured Image', 'your_textdomain' )
                            ,get_the_title()
                            ,'none'
                             // Check your image sizes and adjust this one
                            ,'thumb'
                        );
                    }
                    else
                    {
                        ?>
                        <img src="https://wordpress.stackexchange.com/questions/69602/<?php echo get_template_directory()."/images/no-image.jpg'; ?>" alt="No image available" class="panikosule" />
                        <?php
                    }
                }
                ?>

                <a href="<?php the_permalink() ?>" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
                <?php the_excerpt(); ?> 

                </div>

                <?php
            } // endwhile;
        }
    }
}
wp_reset_query();  // Restore global post data stomped by the_post().
?>
<span class="arrow"></span>
</div>

Leave a Comment