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

(avery) #1

60 PART I • Getting Started with WordPress


This is pretty much the same as the first loop but limited to showing ten posts from the Apples
category. You have a similar check for post IDs used in the featured loop as in the previous
example, but this time you’re checking an array instead. The post output is pretty straightforward,
with every post as an item in the list. Resetting the query with wp_reset_postdata() is
important here; you want a clean slate for your next loop.

There are two more loops, more or less identical to the previous one. The only change is the
category and the variable name where you store WP_Query. Take a look at the whole thing:

<div id="featured">
<?php
// Setting a temporary value to avoid errors
$do_not_duplicate = null;
// Loop arguments
$args = array(
'category_name' => 'featured',
'showposts' => 3
);
// Our featured query and loop
$featured_query = new WP_Query( $args );
while ( $featured_query->have_posts() ) : $featured_query->the_post();
// Save the post ID to $do_not_duplicate
$do_not_duplicate[] = $post->ID;
?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<h2>
<a href="<?php the_permalink(); ?>"
title="<?php the_title_attribute(); ?>">
<?php the_title(); ?>
</a>
</h2>
<?php the_excerpt(); ?>
</div>
<?php
// Featured loop ends
endwhile;
// Resetting
wp_reset_postdata();
?>
</div>
<div class="column-left">
<h2>Latest from <span>Apples</span></h2>
<ul>
<?php
// Loop arguments
$args = array(
'category_name' => 'apples',
'showposts' => 10
);
Free download pdf