Answer the question
In order to leave comments, you need to log in
How to make alternation of posts from different cycles (conditions/taxes)?
Greetings!
There are two types of posts: posts themselves and services, they have different layouts. The task is to display them in one place (sidebar), not one after another, but alternate 2-1-2-2-1, etc. Is it possible to? How?
Answer the question
In order to leave comments, you need to log in
The easiest way is to order specific ids from the database in the order you specify, otherwise you can hardly guarantee that records of different types will come to you in the right order
$args = [
'post_type' => ['post','service'],
'post__in' => [5,12,2,14,7],
'orderby' => 'post__in',
];
$loop = new WP_Query( $args );
if ( get_post_type() === 'service' ) {
get_template_part( 'templates/archive/archive-service' );
} else {
get_template_part( 'templates/archive/archive-common' );
}
$post_type = get_post_type();
if ( file_exists( get_theme_file_path( 'templates/archive/archive-' . $post_type . '.php' ) ) ) {
get_template_part( 'templates/archive/archive-' . $post_type );
} else {
get_template_part( 'templates/archive/archive-common' );
}
Got this response from Kama, for those who are interested:
$tax_name_1 = 'tax_name_1';
$tax_name_2 = 'tax_name_2';
$term_name_1 = 'term_name';
$posts_1 = get_posts( [ параметры_такс_1 ] );
$posts_2 = get_posts( [ параметры_такс_2 ] );
$posts_3 = get_posts( [ параметры_мета ] );
$posts = [];
foreach( $posts_1 as $p ){
$p->tpl_type = $tax_name_1;
$posts[ $p->ID ] = $p;
}
foreach( $posts_2 as $p ){
$p->tpl_type = $tax_name_2;
$posts[ $p->ID ] = $p;
}
foreach( $posts_3 as $p ){
$p->tpl_type = $term_name_1;
$posts[ $p->ID ] = $p;
}
// тут можно отсортировать по ID например
$posts = wp_list_sort( $posts, 'ID', 'DESC' );
foreach( $posts as $post ){
setup_postdata( $post );
if( $post->tpl_type === $tax_name_1 ){
?>
<!-- Вывода постов, функции цикла: the_title() и т.д. -->
<?php
}
elseif( $post->tpl_type === $tax_name_2 ){
?>
<!-- Вывода постов, функции цикла: the_title() и т.д. -->
<?php
}
elseif( $post->tpl_type === $term_name_1 ){
?>
<!-- Вывода постов, функции цикла: the_title() и т.д. -->
<?php
}
else {
echo 'Шаблон для типа не усказан';
}
}
wp_reset_postdata(); // Сбрасываем $post
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question