90 lines
3.1 KiB
PHP
90 lines
3.1 KiB
PHP
<?php
|
|
$current_post_id = is_singular() ? get_queried_object_id() : get_the_ID();
|
|
$post_type = get_post_type($current_post_id);
|
|
$selected_articles = get_field('article_category', $current_post_id) ?: [];
|
|
if (!$selected_articles) {
|
|
$article_field = get_field_object('article_category', $current_post_id, false, false);
|
|
if ($article_field) {
|
|
return;
|
|
}
|
|
// Also detect an available field that has never been saved on this page.
|
|
foreach (acf_get_field_groups(['post_id' => $current_post_id]) as $field_group) {
|
|
foreach (acf_get_fields($field_group) ?: [] as $field) {
|
|
if ($field['name'] === 'article_category') {
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
$selected_ids = [];
|
|
foreach ($selected_articles as $row) {
|
|
$article = $row['post'] ?? null;
|
|
$article_id = $article instanceof WP_Post ? $article->ID : (int) $article;
|
|
if ($article_id > 0 && $article_id !== $current_post_id) {
|
|
$selected_ids[] = $article_id;
|
|
}
|
|
}
|
|
$terms = get_the_terms($current_post_id, 'article_category');
|
|
$related_args = [
|
|
'post_type' => $post_type === 'news' ? 'news' : 'article',
|
|
'posts_per_page' => 6,
|
|
'post__not_in' => [$current_post_id],
|
|
'orderby' => 'date',
|
|
'order' => 'DESC'
|
|
];
|
|
if ($selected_ids) {
|
|
$related_args['post_type'] = 'article';
|
|
$related_args['post__in'] = $selected_ids;
|
|
$related_args['posts_per_page'] = -1;
|
|
$related_args['orderby'] = 'post__in';
|
|
} elseif ($terms && !is_wp_error($terms)) {
|
|
$term_ids = wp_list_pluck($terms, 'term_id');
|
|
$related_args['tax_query'] = [
|
|
[
|
|
'taxonomy' => 'article_category',
|
|
'field' => 'term_id',
|
|
'terms' => $term_ids,
|
|
],
|
|
];
|
|
}
|
|
$related_query = new WP_Query($related_args);
|
|
if ($selected_ids) {
|
|
// WP_Query removes duplicates; restore the exact order and rows from ACF.
|
|
$posts_by_id = [];
|
|
foreach ($related_query->posts as $selected_post) {
|
|
$posts_by_id[$selected_post->ID] = $selected_post;
|
|
}
|
|
$related_query->posts = [];
|
|
foreach ($selected_ids as $selected_id) {
|
|
if (isset($posts_by_id[$selected_id])) {
|
|
$related_query->posts[] = $posts_by_id[$selected_id];
|
|
}
|
|
}
|
|
$related_query->post_count = count($related_query->posts);
|
|
}
|
|
|
|
if ($related_query->have_posts()) :
|
|
$blog_title = get_field('blog_title', $current_post_id);
|
|
$title = $blog_title
|
|
? 'Полезные материалы для ' . $blog_title . ' от Hantico'
|
|
: ($post_type === 'news' ? 'Другие новости' : 'Вам также может понравиться');
|
|
?>
|
|
<section class="articles articles--block articles--slider">
|
|
<div class="container">
|
|
<h2 class="articles-title articles-title--lg"><?php echo esc_html($title); ?></h2>
|
|
<div class="related-slider">
|
|
<div class="related-slider__container swiper">
|
|
<div class="swiper-wrapper">
|
|
<?php
|
|
while ($related_query->have_posts()) {
|
|
$related_query->the_post();
|
|
get_template_part('template-parts/articles-item', null, ['class' => 'swiper-slide']);
|
|
} wp_reset_postdata();
|
|
?>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</section>
|
|
<?php endif; ?>
|