p Вывести похожие записи WordPress — Данил Самигуллин
+7 982 100 5 999

Вывести похожие записи WordPress

<?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; ?>