troubleshooting Question

List Pages by Custom Field - Formatting first 2 differently?

Avatar of mandi224
mandi224Flag for United States of America asked on
1 Comment1 Solution328 ViewsLast Modified:
I'm using the code below (plus the Advanced Custom Fields plugin) to get a listing of sub-pages within events and list them on the homepage of my WordPress site.

There are about 12 pages that are returned, and I want to list them all -- but for the FIRST THREE in the list, I want to display a thumbnail image and apply some different styling. How can I modify the foreach loop (?) to style posts 1, 2, and 3 differently and then display the remaining 9 events styled how they are below?

	$querystr = "
	SELECT * FROM $wpdb->posts
	LEFT JOIN $wpdb->postmeta ON($wpdb->posts.ID = $wpdb->postmeta.post_id)
	LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
	LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
	WHERE $wpdb->posts.post_status = 'publish'
	AND $wpdb->postmeta.meta_key = 'date'
	ORDER BY $wpdb->postmeta.meta_value ASC
	$pageposts = $wpdb->get_results($querystr, OBJECT);

<?php if ($pageposts): ?>
	<?php global $post; ?>
	<?php foreach ($pageposts as $post): ?>
	<?php setup_postdata($post); ?>
		<div class="clearfix">
		<div class="event_listing clearfix" id="post-<?php the_ID(); ?>">
			<span class="event_date"><?php $key="date"; echo get_post_meta($post->ID, $key, true); ?></span>
			<span class="event_title"><a href="/test/events/#<?php the_ID(); ?>" rel="bookmark" title="Permalink"><?php the_title(); ?></a></span>
			<span class="event_location"><?php $key="location"; echo get_post_meta($post->ID, $key, true); ?></span>
	<?php endforeach; ?>
	<?php else : ?>
		<h2 class="center">Not Found</h2>
		<p class="center">Sorry, but you are looking for something that isn't here.</p>
<?php endif; ?>
Ovid Burke
Consultant Instructor

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 1 Comment.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 1 Comment.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros