最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - alternate left - right content inside the loop

programmeradmin3浏览0评论

I'm not sure if this were asked before. I want to obtain a similar effect where the contents and the images are alternated for each post, one time the image is on the left and the text on the right and for the next row the content is displayed with the image on the right and the text on the left. How I can achieve this inside the loop?

I'm not sure if this were asked before. I want to obtain a similar effect where the contents and the images are alternated for each post, one time the image is on the left and the text on the right and for the next row the content is displayed with the image on the right and the text on the left. How I can achieve this inside the loop?

Share Improve this question asked Jan 16, 2020 at 9:54 sialfasialfa 32910 silver badges29 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You should be able to get the current index in loop from the global $wp_query object. With the help of modulo you can then set an alternating css class for a post.

global $wp_query;
while ( have_posts() ) {
  the_post(); 
  $alignment = ( ($wp_query->current_post + 1) % 2 === 0 ) ? 'even align-right': 'odd align-left';

  // post html markup with class="<?php echo $alignment; ?>"
}
发布评论

评论列表(0)

  1. 暂无评论