S
S
smurzak32021-09-17 20:45:41
PHP
smurzak3, 2021-09-17 20:45:41

How to correctly display posts with different layouts?

Hello, while studying wordpress, I encountered such a problem that there are places on the site where posts are displayed with different layouts, here is an example.
6144d3af8b65d491507806.png
These two blocks are displayed with different layout. And there is a third block, which is located almost at the footer, and looks like this
6144d40e1b4ef374838575.png
. How can I display posts in such different layouts?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artem Zolin, 2021-09-18
@smurzak3

For example, you can check the sequence number of the cycle and include the required template

if ( $query->have_posts() ) {
  $i = 1;
  while ( $query->have_posts() ) {
    $query->the_post();
    
    if ( $i == 1 ) {
      get_template_part( 'templates/template-one' ); // первый
    } elseif( $i == 2 ) {
      get_template_part( 'templates/template-two' ); // второй
    } else {
      get_template_part( 'templates/template-common' ); // остальные
    }

    $i++;
  }
} else {
  // Постов не найдено
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question