Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm having difficulties to set the featured image only in the first post of each category and the rest to displayed with the post title only. I manage until now to display the latest post from each category with thumbnail but i cannot find the way to make the first post only with thumbnail and the rest with titles. is there a solution? Here is my code:

                <?php
                    //get terms (category ids 11,2,33,34), then display one post in each term
                    $taxonomy = 'category';//  e.g. post_tag, category
                    $param_type = 'category__in'; //  e.g. tag__in, category__in
                    $term_args=array(
                      'include' => '4,5,6,7',
                      'orderby' => 'name',
                      'order' => 'ASC'
                    );
                    $terms = get_terms($taxonomy,$term_args);
                    if ($terms) {
                      foreach( $terms as $term ) {
                        $args=array(
                          "$param_type" => array($term->term_id),
                          'post_type' => 'post',
                          'post_status' => 'publish',
                          'posts_per_page' => 1,
                          'caller_get_posts'=> 1
                          );
                        $my_query = null;
                        $my_query = new WP_Query($args);
                        if( $my_query->have_posts() ) {
                          //Panikos Remove   echo 'Latest Post in '.$taxonomy .' '.$term->name;
                          while ($my_query->have_posts()) : $my_query->the_post(); ?>
                            <div class="post_home_main">


                            <!--  Add timthumb to post featured image -->
                            <?php
                            $image_id = get_post_thumbnail_id();
                            $image_url = wp_get_attachment_url($image_id);
                            if ($image_url) {
                            ?>
                            <img src="<?php bloginfo('template_directory'); ?>/scripts/timthumb.php?src=<?php echo $image_url; ?>&w=196&h=115&q=1000" alt="<?php the_title();?>" />
                             <!--  // Add timthumb to post featured image -->
                            <?php } else { ?>
                            <img src="<?php echo bloginfo('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>
share|improve this question
    
Please check your spelling (esp. the title). –  kaiser Oct 17 '12 at 23:27

3 Answers 3

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="<?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>
share|improve this answer
    
i get syndax error where it says: ,'none' and <img src="<?php echo get_template_directory().'/images/no-image.jpg'; ?>" alt="No image available" class="panikosule" /> –  user22591 Oct 18 '12 at 5:08
    
@user22591 Forgot a quote - fixed –  kaiser Oct 18 '12 at 13:39

Just before the while ($my_query->have_posts()) add $flag = false;, then wrap your featured image code in another if condition like this

if( !$flag ) {

$flag = true;

// put all you featured image code here

}

Now change the post_per_page parameter to higher number. The image code will only be executed the first time the loop runs for each category

UPDATE

<?php
if( !$flag ) {
    $flag = true;
?>

                        <!--  Add timthumb to post featured image -->
                        <?php
                        $image_id = get_post_thumbnail_id();
                        $image_url = wp_get_attachment_url($image_id);
                        if ($image_url) {
                        ?>
                        <img src="<?php bloginfo('template_directory'); ?>/scripts/timthumb.php?src=<?php echo $image_url; ?>&w=196&h=115&q=1000" alt="<?php the_title();?>" />
                         <!--  // Add timthumb to post featured image -->
                        <?php } else { ?>
                        <img src="<?php echo bloginfo('template_directory') . '/images/no-image.jpg'; ?>" alt="No image available" class="panikosule" />
                        <?php } ?>


<?php } ?>
share|improve this answer
    
Thanks Mridul. But i think i ;m doing domething wrong with the php tags. Im getting an error where is the if( !$flag ) { $flag = true; –  user22591 Oct 17 '12 at 19:37
    
i upadated the answer above –  Mridul Aggarwal Oct 17 '12 at 19:49

Thanks kaiser and Mridul for your reply. I manage to achieve what i want with this code:

<div class="categorys_box">
   <!-- Query only the first post -->
   <?php $cat4 = new WP_Query('cat=4&showposts=1');
      if($cat4->have_posts()) : while($cat4->have_posts()) : $cat4->the_post(); ?>
      <!-- First post only content area -->
      <div class="first_post">

                <?php
                $image_id = get_post_thumbnail_id();
                $image_url = wp_get_attachment_url($image_id);
                if ($image_url) {
                 ?>
                <img src="<?php bloginfo('template_directory'); ?>/scripts/timthumb.php?src=<?php echo $image_url; ?>&w=196&h=115&q=1000" alt="<?php the_title();?>" /> 
                <!--  // Add timthumb to post featured image -->
                <?php } else { ?>
                <img src="<?php echo bloginfo('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><!-- //first_post -->

      <?php endwhile; endif; ?>
      <!-- Query the rest posts. Offset = -1 from the above  -->
      <?php $sidecat4 = new WP_Query('cat=4&showposts=2&offset=1');
      if($sidecat4->have_posts()) : while($sidecat4->have_posts()) : $sidecat4->the_post(); ?>
       <!-- Rest posts content area -->
     <div class="rest_post">

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

        </div><!-- //rest_posts  -->
      <?php endwhile; endif; ?>
      <?php wp_reset_query();  // Restore global post data stomped by the_post().?>
    </div><!-- //categorys_box--> 
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.