Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Matt Campbell
Matt Campbell
9,767 Points

Is there a better way to do this? Creating a page that's divided up into post categories. EG - Meet the Team page

What I'm doing, as the title suggests, is creating a "Meet the Team" page. So, here's how I'm bumbling along doing it. Custom "team" post type. Standard. Put posts into categories that reflect their position. Currently authors and management. Then, I need to get all the authors onto the page and put a sub-heading reflecting the category.

Now, I know I could just put a heading and then run a loop but, imagine if this theme has a new position added? Now, got to go into the code and add another section.

This code is incomplete, but what I plan to do is run a foreach loop cycling through each category getting the posts and echo in the 'category_name' as it goes. Should be simple enough eh?

My issue is at the moment, is I'm having to run two queries to get the title outside the loop, we reckon that's ok?

Here's the code at the moment

<?php $team_category = new WP_Query(array(
        'post_type' => 'team',
        'post_per_page' => 1,
        'category_name' => 'authors',
        )); ?>

        <?php $team_category->the_post(); ?>
        <?php $current_category = get_the_category(); ?>
            <div class="team-position">
                <?php echo $current_category[0]->cat_name; ?>
            </div>

<?php $team_post = new WP_Query(array(
        'post_type' => 'team',
        'post_per_page' => 5,
        'category_name' => 'authors',
        )); ?>

    <?php while($team_post->have_posts()) : $team_post->the_post(); ?>

2 Answers

John Locke
John Locke
15,479 Points

This solution seems like it would be fine, but I would probably just opt for addings a h2 with the category name, and run the loop for the category and post_type inside of that section. If you need to add another position, just add a similar sections with a new loop for that category.

Matt Campbell
Matt Campbell
9,767 Points

Hi John Locke,

Having to go into the code and add something is what I need to avoid, otherwise it would be a piece of cake. Just run a loop for each sub-category.

However, what if I deliver this to a client? Can't have the site "semi-dynamic".

Anyway...dug into the codex last night at about 1am and came up with this, it works perfectly:

<?php
$args = array(
    'type' => 'post',
    'orderby' => 'name',
    'child_of' => 70
  );

$categories = get_categories( $args );

foreach ( $categories as $category ) { ?>
    <div class="bio-row">
    <h2 class="position-title"><?php echo $category->name; ?></h2>

        <?php $q = new WP_Query(array('post_type' => 'team', 'category_name' => $category->name));
        while($q->have_posts()) : $q->the_post(); ?>

            <div class="bio-container">
                <div class="team-picture">
                    <?php the_post_thumbnail(); ?>
                </div>

                <div class="row">
                    <h4 class="team-label">Name: </h4>
                    <p class="team-attribute"><?php echo get_post_meta($post->ID, '_team_name', true); ?></p>
                </div>

                <div class="row">
                    <h4 class="team-label">Age: </h4>
                    <p class="team-attribute"><?php echo get_post_meta($post->ID, '_team_age', true); ?></p>
                </div>

                <div class="team-link">
                    <a href="<?php the_permalink(); ?>">
                        <h2>See Full Bio</h2>
                    </a>
                </div>
            </div>

        <?php endwhile; wp_reset_query(); ?>
    </div>      
<?php } ?>          

Couple of additions are, adding something to make sure management comes out on top etc. Secondly, use a variable of the name of the cat to be translated to id so that isn't so rigid. Then it'll be done.