• Resolved plinth

    (@plinth)


    Hi, I’m using the following code to return the 6 most recent posts from a set category. What I’d like to do is also a thumbnail image for just the first result. Here’s my current code:

    <?php $posts = get_posts( "category=4&numberposts=6" ); ?>
                                                          <?php if( $posts ) : ?>
                                                          <?php foreach( $posts as $post ) : setup_postdata( $post ); ?>
    
                                                  <h3><a href="<?php echo get_permalink($post->ID); ?>" ><?php if (strlen($post->post_title) > 60) {
                                                    echo substr(the_title('', '', FALSE), 0, 150) . '';
                                                    } else {
                                                    the_title();
                                                    } ?></a> <span class="date">(</span><a href="<?php echo get_permalink($post->ID); ?>#comments" ><?php comments_number('0', '1', '%'); ?></a><span class="date">)</span></h3>
                                        <?php the_excerpt(); ?>
                                                          <?php endforeach; ?>
                                                          <?php endif; ?>

    Here’s the code I’ve used eleswhere to show the thumbnail image:

    <a href="<?php the_permalink(); ?>"><img src="<?php echo get_post_meta($post->ID, "image_preview", true);?>" width="130" height="73" alt="<?php the_title(); ?>" /></a>

    But of course if I include it in the first bit of code, they all have a thumbnail image, and I’d like just the first result to have that.

    Thanks!

Viewing 3 replies - 1 through 3 (of 3 total)
  • ?php $posts = get_posts( "category=4&numberposts=6" ); ?>
     <?php if( $posts ) : $count = 0; ?>
    <?php foreach( $posts as $post ) : setup_postdata( $post );
     if($count == 0):
    ?>
    <a href="<?php the_permalink(); ?>"><img src="<?php echo get_post_meta($post->ID, "image_preview", true);?>" width="130" height="73" alt="<?php the_title(); ?>" /></a>
    <?php endif; ?>
    <h3><a href="<?php echo get_permalink($post->ID); ?>" ><?php if (strlen($post->post_title) > 60) {
                                                    echo substr(the_title('', '', FALSE), 0, 150) . '';
                                                    } else {
                                                    the_title();
                                                    } ?></a> <span class="date">(</span><a href="<?php echo get_permalink($post->ID); ?>#comments" ><?php comments_number('0', '1', '%'); ?></a><span class="date">)</span></h3>
    <?php the_excerpt(); ?>
    <?php $count++; endforeach; ?>
    <?php endif; ?>

    Try this once.

    Thread Starter plinth

    (@plinth)

    Thanks chinmoy29, works perfectly!

    You’re welcome.:)

    Thanks
    Chinmoy

    If solve the thread, plz mark it as resolved.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘List recent posts but style first result differently?’ is closed to new replies.