1

I have this piece of code thats pulling through a custom post type clients...

<div class="wrap" style="padding-top: 0 !important;">

    <?php
        $args = array( 
          'post_type' => 'short_courses',
          // 'orderby' => 'none',
          'course_type' => 'digital-marketing'
        );
        $the_query = new WP_Query( $args );
    ?>

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


        <div class="one-third course-item">
            <a href="<?php the_permalink(); ?>">

                <p><img src="<?php the_field('listing_thumbnail'); ?>"></p>
                <h4><?php the_title(); ?></h4> 
                <p style="color: #f08464;"><?php the_field('date_for_thumbnail_listing'); ?></p>  
                <p><?php the_field('price'); ?></p>
                <a href="<?php the_permalink(); ?>" class="sc-box-button" style="background-color: #f08464; margin-bottom: 40px;">MORE INFO</a>

            </a>
        </div>

    <?php $products_count = $the_query->current_post + 1; ?>
    <?php if ( $products_count % 4 == 0): ?>

    <?php endif; ?>


    <?php endwhile; wp_reset_postdata(); endif; ?>

</div>

All working great, but with the bit at the bottom:

<?php $products_count = $the_query->current_post + 1; ?>
<?php if ( $products_count % 4 == 0): ?>

I'm trying to get the items to flow onto the next line after 4 items... currently it's doing it after 3 items...

Apologies if it's something simple I'm missing!!!

***** UPDATE FIXED ******

Sorry - I had achieved this effect with CSS, apologies but it was just down to me changing the CSS element to:

@media (min-width: 576px) {
  .card-columns {
    column-count: 4; }
}
1
  • remove +1 from this line <?php $products_count = $the_query->current_post + 1; ?> and check Commented Jan 10, 2018 at 12:52

3 Answers 3

3

2 ways to do that:-

1.Either Add counter befor loop and then increment it and check(standered+best way)

$products_count = 0;
<?php if ( have_posts() ) :
  //rest code as it is
<?php $products_count++; ?>
<?php if ( $products_count % 4 == 0): ?> //rest code as it is

2.Or Remove +1 from <?php $products_count = $the_query->current_post; ?>

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry - I had achieved this effect with CSS, apologies but it was just down to me changing the CSS element to: @media (min-width: 576px) { .card-columns { column-count: 4; } }
1

I would recommend doing this before the while:

$products_count = 0;

And this for counting:

$products_count++;
if ( $products_count % 4 == 0): ...

1 Comment

Sorry - I had achieved this effect with CSS, apologies but it was just down to me changing the CSS element to: @media (min-width: 576px) { .card-columns { column-count: 4; } }
1
<?php $products_count = $the_query->current_post; ?>

Remove only +1 from the above code.

1 Comment

Sorry - I had achieved this effect with CSS, apologies but it was just down to me changing the CSS element to: @media (min-width: 576px) { .card-columns { column-count: 4; } }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.