Loop for Each Category

Now that you've reset your query, it's time to create that magazine layout. There are various ways to accomplish this, depending on how you've set up your content. Perhaps you've created a hierarchy of pages, and you want to show those instead. For the moment, however, let's assume that you're using categories to segregate your posts into different departments, since this is the most common scenario. Once you see how this works, you should be able to adapt this code to your needs using some of the other Loop examples in this chapter.

Loop One

Loop Two

Loop Three

Loop Four

Loop Five

Loop Six

Loop Seven b

Loop Eight

Loop Nine

Loop Ten

Loop Eleven

Loop Twelve

Figure 7-3. A layout with multiple Loops

You could use get_all_category_ids() to fetch an array of IDs, and then loop through each one. However, this is not a very flexible solution. If you have a deep hierarchy of categories, it's unlikely that you actually want to loop through every one of them on your home page. More likely, you really want a box for every top-level category, as illustrated in Figure 7-3.

In order to better control which categories you'll work with, use the get_categories() function instead, as shown in Listing 7-10. This will require a bit more code, since this function returns an array of category objects rather than simple IDs, but that's OK. You can make use of that object data, and if you really need to pass the ID as a parameter to some other function, hey, that's part of the object, too.

Listing 7-10. Creating a Loop for each top-level category

$categories = get_categories();

<div class="category_box">

// get most recent post in cat query_posts('posts_per_page=1&cat='.$cat->cat_ID);

if (have_posts()) : while (have_posts()) : the_post(); ?>

<h2><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>

<?php the_content('continue reading...'); ?> <?php endwhile; endif;

wp_reset_query();

// get 4 most recent posts in cat offset by 1 query_posts('posts_per_page=4&offset=1&cat='.$cat->cat_ID); if (have_posts()) : ?>

<ul class="more-stories"> <?php while (have_posts()) : the_post(); ?>

<li><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li> <?php endwhile; ?>

</div> <!-- .category_box --> <?php endforeach; ?>

In this example, you use get_categories() to retrieve a list of all the categories. This function can take a number of arguments to limit the list it returns, but in this case you want the complete list. The function returns the categories as objects, so you use the object notation (object->property) to get the category IDs for the query function.

As you loop through each category, call another Loop with the query set to show only posts in that category. Inside this Loop, print the title and excerpt of the first post only. Then you perform yet another query to get the next four posts, skipping the first. (Note that it might be more efficient to get all five posts in one query and use a true/false flag to determine whether the surrounding markup is a paragraph or a list item, but for now, the multiple query demonstration is more important than efficiency.)

You could, of course, modify the above code to create a Loop for all the children of a certain category, or any other scenario you can think of. See codex.wordpress.org/Function_Reference/ get_categories for all the possible arguments of the get_categories() function.

FigUre 7-4. A loop per category
0 0

Post a comment