Lord_Dantes2018-07-11 09:56:28
Lord_Dantes, 2018-07-11 09:56:28

2 Wordpress Post Templates?

There are 2 Wordpress post templates, how you can display posts according to two templates.
In the admin panel, the first post automatically takes the first template, then fill in the second post and automatically take the second template, that is, you need to use post templates in turn for any posts.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Nikolay Mironov, 2018-07-11

I understood the task as follows: display post cards in two different templates in turn. If the problem is different, correct it.
We find the desired template file in the theme (for example, index.php) and the WordPress cycle, it will look something like this:

while (have_posts): the_post();
  //Загружаем шаблон записи
  get_template_part('content', 'single');

Our task is to make sure that in one case one template is loaded in the second case, the second. To do this, we will create template files, such as template-one.php and template-two.php, and move your layout/code there. Let's change the WordPress cycle so that the templates are selected in turn
//Добавим переменную-счетчик, чтобы различать четные и нечетные посты
$counter = 0;
while (have_posts): the_post();
  //добавим проверку на четность
  if ( $counter % 2 == 0 ):
     //загружаем первый шаблон для четных 
     get_template_part('template', 'one');
    //загружаем второй шаблон в другом случае
     get_template_part('template', 'two');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question