Записи с тремя разными шаблонами(WordPress)
Каким образом в WordPress вывести записи с разными шаблонами?
Есть внизу код, но он выводит записи только по первому шаблону(Block-1).
<?php
global $post;
$myposts = get_posts([
'numberposts' => 3,
]);
if( $myposts ){
foreach( $myposts as $post ){
setup_postdata( $post );
?>
<div class ="Block-1 active">
<div class ="Content-left">
<h1 class="title-left"> <?php the_title(); ?> </h1>
<p class="Left_scroll"> <?php the_content(); ?> </p>
</div>
</div>
<?php }} wp_reset_postdata(); ?>
А у меня три таких html-шаблона с разными именами классов дочерних элементов:
<div class ="Main_Block">
<div class ="Block-1 active">
<div class ="Content-left">
<h1 class="title-left"> Заголовок 1 </h1>
<p class="Left_scroll">
Контент(текст)-1
</p>
</div>
</div>
<div class ="Block-2">
<div class ="Content-right">
<h1 class="title-right"> Заголовок 2 </h1>
<p class="Right_scroll">
Контент(текст)-2
</p>
</div>
</div>
<div class ="Block-3">
<div class ="Content-center">
<h1 class="title-center"> Заголовок 3 </h1>
<p class="Center_scroll">
Контент(текст)-3
</p>
</div>
</div>
</div>
Всего должно получится 3 записи с тремя разными шаблонами,⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀ то есть сначала идет Block-1 active > Block-2 > Block-3
Ответы (1 шт):
Автор решения: KAGG Design
→ Ссылка
<?php
global $post;
$my_posts = get_posts(
[ 'numberposts' => 3 ]
);
$positions = [ 'Left', 'Right', 'Center' ];
if ( $my_posts ) {
foreach ( $my_posts as $index => $my_post ) {
setup_postdata( $my_post );
?>
<div class="Block-<?php echo (int) $index + 1; ?> active">
<div class="Content-<?php echo esc_attr( strtolower( $positions[ $index ] ) ); ?>">
<h1 class="title-<?php echo esc_attr( strtolower( $positions[ $index ] ) ); ?>">
<?php the_title(); ?>
</h1>
<p class="<?php echo esc_attr( $positions[ $index ] ); ?>_scroll"><?php the_content(); ?></p>
</div>
</div>
<?php
}
}
wp_reset_postdata();