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

Punal Chotrani
Punal Chotrani
8,817 Points

Add a condition to a wordpress loop?

Hi Guys,

I have a standard wordpress loop, and my theme is built with bootstrap. I'm using the wp_query loop to get all the data i need, and i have also set the condition of the loop to add every 2 posts in a

<div class=row></div> 

I need to give it a condition if the post count is odd using something like this.

<?php
    if ($loop->post_count == odd) {
       //do something
    } 
?>

then insert a

<div class="col-md-6 col-sm-height">
&nbsp;</div>

inside the last row of the loop. to look something like this.

<div class=row>
   <div class="col-md-6 col-sm-height">&nbsp;</div>
</div>

Here is my loop code

      <?php
            $page_id = (is_front_page() ? 0 :get_the_ID()); //Gets the id for the current page.
            $childpages = new WP_Query(array(
                    'post_type' => 'page',
                    'post_parent' => $page_id,
                    'posts_per_page' => -1,
                    'post__not_in' => array(get_option('page_on_front')),
                    'orderby' => 'menu_order date',
                    'order' => 'ASC'
                )
            );
            ?>
            <?php
            $loop = $childpages;
            if( $loop->have_posts() ):
            $counter = 0;
            while ( $loop->have_posts() ) : $loop->the_post();
            ?>
            <?php
            $counter++;
            if ( $counter % 2 != 0 ): ?>
            <div class="row">
                <div class="row-height">

                            <?php endif; ?>
                            <div class="col-md-6 col-sm-height">
                                <div class="inside inside-full-height">
                                    <div class="content">
                                    <article>
                                        <div class="entry-header">
                                            <h2>
                                                <a href="<?php echo make_path_relative(get_page_link()); ?>">
                                                    <?php the_title(); ?>
                                                </a>
                                            </h2>
                                        </div>
                                        <div class="entry-content clearfix">
                                            <?php $content(); ?>
                                        </div>
                                    </article>
                                    </div>
                                </div>
                            </div>
                <?php

                if ( $counter % 2 == 0 ): ?>
                        </div>
                    </div>
                <?php endif; ?>
                <?php
                    //if($count%2==1)
                    if( $loop->post_count%2==1 )
                        echo '<div class="col-md-6 col-sm-height">
                        &nbsp;
                        </div>';
                    ?>

                    <?php //$counter++;
                    endwhile; ?>
                    <?php endif; ?>

I hope it's clear what i'm trying to achieve. Any help regarding this would be really helpful. Thanks for having a look.