Files
vij/blocks/pets.php

51 lines
1.4 KiB
PHP
Raw Normal View History

2025-04-22 00:54:04 +03:00
<?php
// Задаем ID нужной категории (родительской)
$parent_cat_id = 14; // замените на нужный ID рубрики
// Получим ID подкатегорий
$subcats = get_categories( array(
'child_of' => $parent_cat_id,
'hide_empty' => false,
) );
// Собираем все ID: родительская + подкатегории
$category_ids = array( $parent_cat_id );
foreach ( $subcats as $cat ) {
$category_ids[] = $cat->term_id;
}
// Получаем 3 случайных поста из этих категорий
$args = array(
'category__in' => $category_ids,
'posts_per_page' => 3,
'post_status' => 'publish',
'orderby' => 'rand'
);
$random_posts = get_posts( $args );
// Если нашли посты
if ( $random_posts ) {
// Выбираем случайный один из трёх
$post = $random_posts[ array_rand( $random_posts ) ];
setup_postdata( $post );
?>
<div class="pets-widget">
<div class="pets-widget-maintitle"><a href="/pets">Питомцы</a></div>
<a href="<?php the_permalink(); ?>">
<?php if ( has_post_thumbnail() ) : ?>
<div>
<?php the_post_thumbnail('medium'); ?>
</div>
<?php endif; ?>
<h3 class="pets-widget-title"><?php the_title(); ?></h3>
</a>
</div>
<?php
wp_reset_postdata();
}
?>