Title: Featured posts? Multiple loops?
Last modified: August 19, 2016

---

# Featured posts? Multiple loops?

 *  [olafg1](https://wordpress.org/support/users/olafg1/)
 * (@olafg1)
 * [16 years, 11 months ago](https://wordpress.org/support/topic/featured-posts-multiple-loops/)
 * Hey,
 * I’m making a blog, I want the most recent post to be featured. I want this to
   go automaticly, I found something on nettuts, but seems like that doesn’t work
   on versions later than 2.7
 * This is what I want to make:
    [http://dznr.org/haj7](http://dznr.org/haj7)
 * Is there a way to do this with code and not plugins? I want it to go automatcily

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

 *  [@mercime](https://wordpress.org/support/users/mercime/)
 * (@mercime)
 * [16 years, 11 months ago](https://wordpress.org/support/topic/featured-posts-multiple-loops/#post-1147147)
 * That looks almost exactly like Derek Punsalan’s The Unstandard Theme
    = [http://theunstandard.5thirtyone.com/](http://theunstandard.5thirtyone.com/)
   Download – [http://5thirtyone.com/the-unstandard](http://5thirtyone.com/the-unstandard)
 *  Thread Starter [olafg1](https://wordpress.org/support/users/olafg1/)
 * (@olafg1)
 * [16 years, 11 months ago](https://wordpress.org/support/topic/featured-posts-multiple-loops/#post-1147150)
 * Thank you very much! I will look into the code:D
 *  Thread Starter [olafg1](https://wordpress.org/support/users/olafg1/)
 * (@olafg1)
 * [16 years, 11 months ago](https://wordpress.org/support/topic/featured-posts-multiple-loops/#post-1147151)
 * The code is way to complicated to bring over to another theme. Is there no simpler
   way of doing this?
 *  [RGlover](https://wordpress.org/support/users/rglover/)
 * (@rglover)
 * [16 years, 11 months ago](https://wordpress.org/support/topic/featured-posts-multiple-loops/#post-1147205)
 * The code from The Unstandard theme isn’t particularly difficult to port over.
   It’s actually a pretty clever way of accomplishing what you want to do.
 * The critically important bit of code that you should be concerning yourself with
   is this:
 *     ```
       $postcount = array();
       function is_first_post($id)
       {
       	global $postcount;
       	# Add a new id if one has not been added already
       	$postcount[$id] = true;
       	# If we're on the first page of posts and the first post
       	if ( is_home() && !is_paged() && count($postcount) == 1 )
       	return true;
       	return false;
       }
       ```
   
 * in functions.php. That is the loop that determines whether or not the post in
   question is the first one on the page. It is used like this:
 *     ```
       <?php echo ( is_first_post($post->ID) ) ? 'main-post-bg' : 'secondary-post-bg left'; ?>
       ```
   
 * to echo the appropriate class for the post (if the post ID in question is the
   first post on the page, the post gets a class of main-post-bg. If not, the post
   gets a class of secondary-post-bg left. He also uses the conditional again along
   with the get_post_meta() function to grab the appropriately-sized version of 
   the post image.
 *     ```
       <?php if ( is_first_post($post->ID) ) $img_src = get_post_meta($post->ID, 'lead_image', true);
                           else $img_src = get_post_meta($post->ID, 'secondary_image', true);
                           if ( $img_src == '' ) $img_src = '/wp-content/themes/theunstandard/images/theunstandard-blank.png';
                       ?>
       ```
   
 * That strikes me as a pretty straightforward method of accomplishing exactly what
   it is you’re looking to do. All you’d need to do is style the relevant CSS classes,
   and add a lead_image and secondary_image custom field to each post containing
   a url for the lead and secondary images.
 * Cheers.
 *  Thread Starter [olafg1](https://wordpress.org/support/users/olafg1/)
 * (@olafg1)
 * [16 years, 11 months ago](https://wordpress.org/support/topic/featured-posts-multiple-loops/#post-1147292)
 * Thanks, but it seems like this is way over my head….
 * I tried altering the code to my own needs, copying it straight over from the 
   theme but I still can’t make it work…
 *  [RGlover](https://wordpress.org/support/users/rglover/)
 * (@rglover)
 * [16 years, 11 months ago](https://wordpress.org/support/topic/featured-posts-multiple-loops/#post-1147307)
 * What’s not working? Are you getting errors? Where did you put the code? How are
   you styling it? Did you create appropriate custom fields?
 * This is almost a cut and paste job, but not quite. There are sufficient steps
   in between to making your theme look the way you want it to.
 *  Thread Starter [olafg1](https://wordpress.org/support/users/olafg1/)
 * (@olafg1)
 * [16 years, 11 months ago](https://wordpress.org/support/topic/featured-posts-multiple-loops/#post-1147308)
 * I tried wihtout styling them first, to see that it would print at all, when I
   refresh I only get a blank page, and this is a code in index.php
 *     ```
       <?php echo ( is_first_post($post->ID) ) ? 'main-post-bg' : 'secondary-post-bg left'; ?>
   
       <?php if ( is_first_post($post->ID) ) $img_src = get_post_meta($post->ID, 'lead_image', true);
        else $img_src = get_post_meta($post->ID, 'secondary_image', true);
        if ( $img_src == '' ) $img_src = '/wp-content/themes/casey/images/theunstandard-blank.png';
       ?>
   
                       <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
                       <?php if ( is_first_post($post->ID) ) { ?>
                       <img src="<?php bloginfo('template_directory'); ?>/scripts/timthumb.php?src=<?php echo $img_src; ?>&w=593&h=225&zc=1" width="593" height="225" />
                       <?php } else { ?>
                       <img src="<?php bloginfo('template_directory'); ?>/scripts/timthumb.php?src=<?php echo $img_src; ?>&w=293&h=150&zc=1" width="293" height="150" />
                       <?php } ?>
                       </a>
   
       <h3><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>
   
       <?php endwhile; ?>
   
       <?php else : ?>
   
       <?php endif; ?>
       ```
   
 *  [RGlover](https://wordpress.org/support/users/rglover/)
 * (@rglover)
 * [16 years, 11 months ago](https://wordpress.org/support/topic/featured-posts-multiple-loops/#post-1147311)
 * post your index.php file and your functions.php file in the [WordPress Pastebin](http://wordpress.pastebin.ca/)
   and post the links here. I’ll have a look and see if I can help.
 *  Thread Starter [olafg1](https://wordpress.org/support/users/olafg1/)
 * (@olafg1)
 * [16 years, 11 months ago](https://wordpress.org/support/topic/featured-posts-multiple-loops/#post-1147313)
 * The only thing I need help with is the grid thing, to be able to display it as
   I showed above:
 * index.php:
 * [http://wordpress.pastebin.ca/1500267](http://wordpress.pastebin.ca/1500267)
 * functions.php
    [http://wordpress.pastebin.ca/1500268](http://wordpress.pastebin.ca/1500268)
 * EDIT: Also, what do I need to do to make it only display posts from one category?
 *  [RGlover](https://wordpress.org/support/users/rglover/)
 * (@rglover)
 * [16 years, 11 months ago](https://wordpress.org/support/topic/featured-posts-multiple-loops/#post-1147317)
 * Your problem is that you haven’t started the loop to get any content for your
   is_first_post() function to test.
 * I haven’t tested it, but this:
 * [http://wordpress.pastebin.ca/1500285](http://wordpress.pastebin.ca/1500285)
 * ought to do the job.
    —-
 * As for displaying posts from one category, you need to use the [query_posts()](http://codex.wordpress.org/Template_Tags/query_posts#Category_Parameters)
   function when building the loop.
 *  Thread Starter [olafg1](https://wordpress.org/support/users/olafg1/)
 * (@olafg1)
 * [16 years, 11 months ago](https://wordpress.org/support/topic/featured-posts-multiple-loops/#post-1147318)
 * Thank you so much!
 * I’m trying to simplify it a bit, so that you would only have one image custom
   field. This is what I changed:
 * [http://wordpress.pastebin.ca/1500307](http://wordpress.pastebin.ca/1500307)
 * I changed lead_image and secondary_image to image, image contains an url to the
   image itself, as you can see here: [http://dev.olafg.com/shop/](http://dev.olafg.com/shop/)
 * It will not print the lead_image
 *  [RGlover](https://wordpress.org/support/users/rglover/)
 * (@rglover)
 * [16 years, 11 months ago](https://wordpress.org/support/topic/featured-posts-multiple-loops/#post-1147319)
 * That makes pretty good sense. I thought about that myself when I was working 
   on it, as I noticed you were using timthumb.
 * As for why it isn’t printing the large image, You’re missing an echo statement
   in the first if block.
 * [http://wordpress.pastebin.ca/1500316](http://wordpress.pastebin.ca/1500316)
 *  Thread Starter [olafg1](https://wordpress.org/support/users/olafg1/)
 * (@olafg1)
 * [16 years, 11 months ago](https://wordpress.org/support/topic/featured-posts-multiple-loops/#post-1147320)
 * Yeah, thanks for all your help, man. Really, thanks. I wonder why the lead image
   won’t print the image for the first post.
 *  [RGlover](https://wordpress.org/support/users/rglover/)
 * (@rglover)
 * [16 years, 11 months ago](https://wordpress.org/support/topic/featured-posts-multiple-loops/#post-1147321)
 * Sorry, I was editing when you replied. Have a look at the URL above. That should
   fix the problem.

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

The topic ‘Featured posts? Multiple loops?’ is closed to new replies.

 * 14 replies
 * 3 participants
 * Last reply from: [RGlover](https://wordpress.org/support/users/rglover/)
 * Last activity: [16 years, 11 months ago](https://wordpress.org/support/topic/featured-posts-multiple-loops/#post-1147321)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
