Цикл wordpress: как добавить css определенным по счету постам?

Здравствуйте! Есть такой вот цикл в теме wordpress:
<main id="main" class="site-main" role="main">


			<?php if ( have_posts() ) : ?>
	
				<?php /* Start the Loop */ ?>
				<div class="posts-loop">
				<?php while ( have_posts() ) : the_post(); ?>
	
					<?php
						/* Include the Post-Format-specific template for the content.
						 * If you want to override this in a child theme, then include a file
						 * called content-___.php (where ___ is the Post Format name) and that will be used instead.
						 */
						get_template_part( 'template-parts/' . $post_template );
					?>
	
				<?php endwhile; ?>
				</div><!-- / .posts-loop -->
				
				<div class="postnav">
					<?php if(function_exists('wp_pagenavi')) { wp_pagenavi(); } ?>
				</div>
	
			<?php else : ?>
	
				<?php get_template_part( 'template-parts/content', 'none' ); ?>
	
			<?php endif; ?>

		</main><!-- #main -->

А вот содержимое файла content.php, куда нужно добавить CSS для постов.
<article id="post-<?php the_ID(); ?>" <?php post_class( 'list-post grid_item' ); ?> >
Здесь вся информация постов и т.д.
</article>

Необходимо добавить CSS, скажем, "custom-post" для каждой 5-й записи (или любой другой, 6, 7....)
<article id="post-<?php the_ID(); ?>" <?php post_class( 'list-post grid_item' ); ?> >


Важное уточнение! Класс "custom-post" необходимо добавить, начиная с первого поста и дальше каждый 5-й. Получается css класс должен добавляться таким по счету постам: 1, 5, 10 и т.д.

Нашел простое вроде бы решение, но не знаю рабочее ли оно для моей ситуации, и не знаю куда всталять код.
Outside your loop:
$classes = array(
    0=>'first-column',
    1=>'second-column',
    2=>'third-column'
);
$i = 0;

In your loop:
<article id="post-<?php the_ID(); ?>" <?php post_class($classes[$i++%3]); ?>>

output like :
<article class="first-column format-standard hentry category-uncategorized">
<article class="second-column format-standard hentry category-uncategorized">
<article class="third-column format-standard hentry category-uncategorized">
<article class="first-column format-standard hentry category-uncategorized">
<article class="second-column format-standard hentry category-uncategorized">
<article class="third-column format-standard hentry category-uncategorized">
  • Вопрос задан
  • 696 просмотров
Решения вопроса 1
wppanda5
@wppanda5 Куратор тега WordPress
WordPress Mедведь
Пригласить эксперта
Ответы на вопрос 1
Создаете обычный счетчик $k = 0
После чего проверяете условие:
if($k%5 == 0) {

}

Если выполняется - добавляете класс. В общем все.

Раз ноль, то вот:
<main id="main" class="site-main" role="main">


      <?php $k = 0;
       if ( have_posts() ) : ?>
  
        <?php /* Start the Loop */ ?>
        <div class="posts-loop">
        <?php while ( have_posts() ) : the_post(); ?>
  
          <?php
            /* Include the Post-Format-specific template for the content.
             * If you want to override this in a child theme, then include a file
             * called content-___.php (where ___ is the Post Format name) and that will be used instead.
             */
            get_template_part( 'template-parts/' . $post_template );
          ?>
  
        <?php endwhile; ?>
        </div><!-- / .posts-loop -->
        
        <div class="postnav">
          <?php if(function_exists('wp_pagenavi')) { wp_pagenavi(); } ?>
        </div>
  
      <?php else : ?>
  
        <?php get_template_part( 'template-parts/content', 'none' ); ?>
  
      <?php endif; ?>

    </main><!-- #main -->

Файл content.php
<?php if($k%5 == 0){ post_class( 'list-post grid_item' );} else {post_class( 'list-post grid_item' );} ?> 
Здесь вся информация постов и т.д.
</article>
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы