V
V
VPVPVP2020-08-29 19:02:08
WordPress
VPVPVP, 2020-08-29 19:02:08

Different classes for WordPress posts?

I display posts on the main page through

<?php
$args = array( 'post_type' => 'post', 'posts_per_page' => 2 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
get_template_part( 'article' );
endwhile; ?>

In article.php I have a class
<article class="news__item content-item item--bg-right flex">

I would like to have a different class for the 2nd post (conditionally in a checkerboard pattern)

Well, that is, post 1/3/5, etc..-
<article class="news__item content-item item--bg-right flex">

Post 2/4/6 etc...-
<article class="news__item content-item content-item--left news__item--divider-left flex">


5f4a7d29708a0102914446.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Litvinenko, 2020-08-29
@VPVPVP

I will not write specifically for your example, I think you will take the idea and redo it for yourself, with a class it is even easier

<?php if ( have_posts() ) : $ir = 0; while ( have_posts() ) : the_post();

  if ( $ir % 2 == 0 ) {
    get_template_part( 'template-parts/article-wide' );
  } else {
    get_template_part( 'template-parts/article' );
  }

$ir++; endwhile; endif; ?>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question