Thord Daniel Hedengren - Smashing WordPress_ Beyond the Blog-Wiley (2014)

(avery) #1

62 PART I • Getting Started with WordPress


// Resetting
wp_reset_postdata();
?>
</ul>
</div>
<div class="column-right">
<h2>Latest from <span>Lemons</span></h2>
<ul>
<?php
// Loop arguments
$args = array(
'category_name' => 'lemons',
'showposts' => 10
);
// Our query and loop
$lemon_query = new WP_Query( $args );
while ( $lemon_query->have_posts() ) : $lemon_query->the_post();
// Let's not output any featured posts
if ( in_array( $post->ID, $do_not_duplicate ) ) continue;
update_post_caches( $posts );
?>
<li>
<h3>
<a href="<?php the_permalink(); ?>"
title="<?php the_title_attribute(); ?>">
<?php the_title(); ?>
</a>
</h3>
<?php the_excerpt(); ?>
</li>
<?php
// Loop ends
endwhile;
// Resetting
wp_reset_postdata();
?>
</ul>
</div>

There you have it: four loops starting with one for featured posts and then three more to
display the latest from three different categories. This code can easily be implemented in just
about any WordPress theme, but you would need to alter it to fit your category setup to make
it work properly.

GETTING MORE COMFORTABLE WITH THE LOOP


The loop can certainly feel tricky to work with at first. The basic loop isn’t too hard to identify,
and that is really all you need to get started with WordPress. With a little practice, you will
find it easier to understand the loop’s mechanics, and when you start to build your own
themes to power your WordPress site, you’ll soon find yourself fiddling with it. The fact that a
Free download pdf