1

I've created a post array inside of the homepage of my clients site. Basically, it's meant to display the six most recent posts of the blog. Now, the client wants the third of those six to be another div with an imag inside of it. In other words, static content. So is there a way I can incorporate some kind of if/else statement that after the second post, it displays an img instead of the post?

Here's what I have for now:

<?php $args = array( 'tag' => 'Home', 'posts_per_page' => '6', );
                $queryone = new WP_Query( $args ); ?>
                    <?php while ($queryone -> have_posts()) : $queryone -> the_post(); ?>
                <section class="feature-third">
                <a href="<?php echo get_permalink( $ID ); ?>" title="<?php the_title( ); ?>">
                <div class="feature-captions"><?php $current_cat_id = the_category_ID(false); echo '<p class="category-link">' . get_cat_name($current_cat_id) . '</p>';  ?><h2><?php echo get_the_title( $post->ID ); ?> </h2><?php if( get_field('subtitle') ): ?><p><?php the_field('subtitle'); ?></p><?php endif; ?></div>
            <?php the_post_thumbnail('full'); ?>
        </a>
    </section>
<?php endwhile; ?>
1
  • 1
    This is a very basic php question. Nothing to do with WordPress.
    – cjbj
    Commented Jun 24, 2017 at 13:30

1 Answer 1

1

You can add your div every after 2 posts like this.

  <?php $count = 1; // add this before loop ?> 

 <?php
  // add this inside the loop
 if ($count % 3 == 0) : 
                    echo 'YOUR IMAGE DIV HERE';
 endif; $count++; ?>

So, it will be something like this.

        <?php $args = array( 'post_type' => 'galleries');
                    $queryone = new WP_Query( $args ); 
                    $count = 1; 
                 while ($queryone -> have_posts()) : $queryone -> the_post(); ?>
    <?php if ($count % 3 == 0) : 
                        echo 'YOUR IMAGE DIV HERE';
     endif; $count++; ?>
                    <section class="feature-third">
                    <a href="<?php echo get_permalink( $ID ); ?>" title="<?php the_title( ); ?>">
                    <div class="feature-captions"><?php $current_cat_id = the_category_ID(false); echo '<p class="category-link">' . get_cat_name($current_cat_id) . '</p>';  ?><h2><?php echo get_the_title( $post->ID ); ?> </h2><?php if( get_field('subtitle') ): ?><p><?php the_field('subtitle'); ?></p><?php endif; ?></div>
                <?php the_post_thumbnail('full'); ?>
            </a>
        </section>
    <?php endwhile; ?>
2
  • Thanks. That worked great. Just added the new code now and altered the array from 6 to 5. Worked.
    – Adam Bell
    Commented Jun 24, 2017 at 21:42
  • Obligatory mention that query already counts for you via $query->current_post property, no need for explicit counting in most cases. :)
    – Rarst
    Commented Jun 26, 2017 at 15:35

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