Files
agroexpert/partials/forest-informer.php

60 lines
1.8 KiB
PHP
Raw Normal View History

2025-05-29 14:28:34 +03:00
<?php
// Задаем ID нужной категории (родительской)
$parent_cat_id = 3691; // замените на нужный 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 случайных поста из этих категорий
$params = array(
'category__in' => $category_ids,
'posts_per_page' => 3,
'post_status' => 'publish',
'orderby' => 'rand',
'date_query' => array(
array(
2025-07-15 18:12:58 +03:00
//'after' => '3 month ago',
'after' => 'July 14, 2025',
2025-05-29 14:28:34 +03:00
'inclusive' => true,
),
),
);
$random_posts = get_posts( $params );
// Если нашли посты
if ( $random_posts ) {
// Выбираем случайный один из трёх
$post = $random_posts[ array_rand( $random_posts ) ];
setup_postdata( $post );
?>
<div class="agro-widget<?if ($args['subclass'] !== '') echo ' '.$args['subclass'];?>">
<div class="agro-widget-maintitle"><a href="/forest">Международная научно-практическая конференция<br>«Защита леса защита будущего»</a></div>
<a href="<?php the_permalink(); ?>">
<?php if ( has_post_thumbnail() ) : ?>
<div>
<?php the_post_thumbnail('full'); ?>
</div>
<?php endif; ?>
<h3 class="agro-widget-title"><?php the_title(); ?></h3>
</a>
</div>
<?php
wp_reset_postdata();
}
?>