Avatar of mandi224
Flag for United States of America asked on

List Pages by Custom Field - Formatting first 2 differently?

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; ?>

Open in new window


Avatar of undefined
Last Comment
Ovid Burke

8/22/2022 - Mon
Ovid Burke

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Your help has saved me hundreds of hours of internet surfing.