OK, I creating homepage that have 4 latest posts, and each of them must have different class to style them using CSS.
I am using this code to display 4 latest posts:
<?php
// the query
$the_query = new WP_Query( array(
'category_name' => 'artykuly',
'posts_per_page' => 4,
'order' => 'ASC',
));
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<!---posty home--->
<div class="home-posty" style="background: #ccc; margin: 20px;">
<a href="<?php echo esc_url( get_permalink() ); ?>"><?php the_title(); ?></a>
<div class="home-post-opis">
<?php the_excerpt(); ?>
</div>
<div class="home-post-tags">
<?php the_tags(); ?>
</div>
<div class="home-post-date">
<?php echo get_the_date(); ?>
</div>
</div>
Question is how to set for lates 4 post different class like: "first-post", "second-one", "third-post", "last-one". It can't be post-id or title because every new post will have different ID and layout must be always the same. How to force adding my own classes to them? I was thinking about CSS nth-child but custom classess will be better IMHO.
I also need to wrap (using DIV) first two of them. Is it possible?
$i++each iteration and then check and do<p class="your-class-<?php echo $i; ?>"><?php echo get_the_excerpt(); ?></p><- quick and dirty$the_query->current_postif you want to avoid messing the$ivariable.