0

I'm setting up a query for a series of 12 classes that will last a week each. There will be a morning session and an evening session. I'll have two different loops in the template to break those apart. For each session there are three project choices the students will work on and to further complicate matters, these classes will repeat once. The first one happening in June, and the second in July. The code below seems to be working. I'm able to first filter down to the project, and then whether it is a June Session or July session and either display the entire post or just the link to the registration.

My problem is that I'll have to repeat the code within the if(in_category('16')) three times for each loop (with a different cat number) in order to make this work. I feel like I should be able to replace this with some sort of foreach or while statement that will iterate over and array of category id's. Something like if(in_category(array('16', '17', '18')) and then somehow go through each one, one at a time executing the code below it. I'm guessing I'd have to assign the current cat id to a variable and pass it into the get_term_by so that my counter would still work if that's possible. Any help is much appreciated!

<?php
    $args = array(
        'post_type' => 'summerartcamp',
        'cat' => 15, //This is for the Morning Group - I'll have a second loop for the Afternoon Group
        'order' => 'ASC',
        'meta_key' => '_expiration-date',
        'orderby' => 'meta_value'
    );
    $the_query = new WP_Query( $args );
    ?>
    <?php if (have_posts()) : ?>
    <?php while ( $the_query->have_posts()) : $the_query->the_post(); ?>

        <?php if (in_category('16')) { // Can I replace this with some sort of array of categories that I can iterate through? ?> 
        <?php $postsInCat = get_term_by('id', '16', 'category');
        $postsInCat = $postsInCat->count;
            if($postsInCat == 2) { // If the number of posts is two I only need to show the button for the second post
                if (in_category('22')) {
                    echo 'This is the first post';
                    echo "<h2>" . get_the_title() . "</h2>";
                }
                if (in_category('23')) {
                    echo 'This is the second post';
                }
            }
            if($postsInCat == 1) { // If there is only one post I'll give the full post
                echo "<h2>" . get_the_title() . "</h2>";
            } ?>
        <?php } ?>          

    <?php
    endwhile; endif;
    ?>
0

In short, yes!

$args = array(
    'post_type' => 'summerartcamp',
    'cat' => 15, //This is for the Morning Group - I'll have a second loop for the Afternoon Group
    'order' => 'ASC',
    'meta_key' => '_expiration-date',
    'orderby' => 'meta_value'
);

$the_query = new WP_Query( $args );

if ( $the_query->have_posts() /* Make sure you use the method, not the global function have_posts(), which only applies to the main loop */ ) {
    while ( $the_query->have_posts() ) {
        $the_query->the_post();

        foreach ( array( 16, 17, 18 ) as $the_cat_id ) {
            if ( in_category( $the_cat_id ) ) {
                $postsInCat = get_term_by( 'id', $the_cat_id, 'category' );

                if ( $postsInCat->count == 2 ) { // If the number of posts is two I only need to show the button for the second post
                    if (in_category( 22 ) ) {
                        echo 'This is the first post';
                        echo "<h2>" . get_the_title() . "</h2>";
                    }

                    if ( in_category( 23 ) ) {
                        echo 'This is the second post';
                    }

                } else if ( $postsInCat->count == 1 ) { // If there is only one post I'll give the full post
                    echo "<h2>" . get_the_title() . "</h2>";
                }
            }
        }
    }
}
  • This works beautifully. It's uncovered an error in my code. Where there are two posts in the category the second post is being spit out below all the other posts, instead of being inserted along with the other post sharing the category 16, 17, or 18. – user2530671 Apr 10 '15 at 20:04
  • In which case you'll need to rethink your approach - perhaps add the output to an array stack in the first loop (rather than echo'ing), and then looping over again to echo. – TheDeadMedic Apr 11 '15 at 9:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.