<?php
$categories = get_the_category($post->ID);
if ($categories) :
$category_ids = array();
foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;
$args=array(
'category__in' => $category_ids,
'post__not_in' => array($post->ID),
'showposts' => '3',
'orderby' => 'rand',
'ignore_sticky_posts' => '1',
'no_found_rows' => true,
'cache_results' => false
);
$my_query = new wp_query($args);
if( $my_query->have_posts() ) : ?>
<div class="sample-posts">
<div class="container">
<div class="title-block">Похожие статьи</div>
<div class="sample_block">
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<a class="news--inner_sample" href="">
<div class="miniature">
<?php
$autoimage = wpschool_first_post_img();
if( has_post_thumbnail() ) {
the_post_thumbnail('large');
}
else {
echo '<img src="' . $autoimage . '" alt="" />';
}?>
</div>
<div class="title">
<h2><?php the_title(); ?></h2>
</div>
<div class="descr">
<p><?php echo trim_characters(180, '...'); ?></p>
</div>
<div class="btn-block">
<span class="btn" href="<?php the_permalink(); ?>">Читать далее</span>
</div>
</a>
<?php endwhile; ?>
</div>
</div>
</div>
<?php endif; wp_reset_query(); endif; ?>
Вывести похожие записи из пользовательской таксономии из той категории, где находится эта запись
<?php
$categories = get_terms(array(
'taxonomy' => 'objectscat',
'hide_empty' => false,
'exclude' => 1,
'orderby' => 'ID',
'order' => 'ASC'
));
if (!empty($categories) && !is_wp_error($categories)) : ?>
<section class="recomendation opacity-anim">
<div class="container">
<div class="title-cont flex_block">
<div class="title-big opacity-anim">Посмотрите так же</div>
<div class="slider-nav-cont opacity-anim">
<div class="slider-nav-item slider-prev"></div>
<div class="slider-nav-item slider-next"></div>
</div>
</div>
<div class="slider-cont opacity-anim">
<div class="recomendation-slider swiper">
<div class="swiper-wrapper">
<?php foreach ($categories as $cat) :
$category_link = get_term_link($cat);
$imgcat = get_field('обложка', $cat);
if (!is_wp_error($category_link)) : ?>
<a class="product-item miniature swiper-slide" href="<?php echo esc_url($category_link); ?>">
<div class="num content-title"><?php the_field('дополнительное_название', $cat); ?></div>
<?php if (!empty($imgcat)) : ?>
<img
srcset="<?php echo esc_url($imgcat['sizes']['large']); ?> 1000w"
sizes="(max-width: 1000px) 100vw, 1000px"
src="<?php echo esc_url($imgcat['url']['medium_large']); ?>"
alt="<?php echo esc_attr($cat->name); ?>"
title="<?php echo esc_attr($cat->name); ?>"
loading="lazy"
itemprop="image"
>
<?php endif; ?>
<div class="text">
<div class="title-block">[<?php the_field('адрес', $cat); ?>]</div>
<div class="metro"><?php the_field('метро', $cat); ?></div>
<div class="price"><?php the_field('стоимость', $cat); ?></div>
</div>
</a>
<?php endif;
endforeach; ?>
</div>
</div>
</div>
</div>
</section>
<?php endif; ?>