add new logic rubrics

This commit is contained in:
argoexpert press
2025-05-10 00:00:09 +03:00
parent 9ff9de46a4
commit 64154c3dc5
7 changed files with 136 additions and 22 deletions

View File

@@ -2,7 +2,7 @@
<div class="content-middle articles-wrapper"> <div class="content-middle articles-wrapper">
<?php include(get_template_directory().'/partials/pinned-post-mob.php');?> <?php include(get_template_directory().'/partials/pinned-post-mob.php');?>
<?php get_template_part('partials/rubrics-mobile'); ?> <?php get_template_part('partials/rubrics-mobile-menu'); ?>
<div class="articles-preview"> <div class="articles-preview">
<?php <?php
$args = array( $args = array(

View File

@@ -1,6 +1,8 @@
<?php <?php
setlocale(LC_TIME, 'ru_RU.UTF-8'); setlocale(LC_TIME, 'ru_RU.UTF-8');
require get_template_directory() . '/inc/rubrics-menu.php'; // Рубрики в меню
// Полностью отключить XML-RPC // Полностью отключить XML-RPC
add_filter( 'xmlrpc_enabled', '__return_false' ); add_filter( 'xmlrpc_enabled', '__return_false' );

63
inc/rubrics-menu.php Normal file
View File

@@ -0,0 +1,63 @@
<?php
function get_categories_from_menu_exact_order($menu_name) {
$cache_key = 'categories_from_menu_' . $menu_name;
$cache_group = 'menu_categories';
// Попытка получить кэш
$cached = wp_cache_get($cache_key, $cache_group);
if ($cached !== false) {
return $cached;
}
// Получение пунктов меню
$menu_items = wp_get_nav_menu_items($menu_name);
if (!$menu_items) return [];
$category_ids = [];
foreach ($menu_items as $item) {
if ($item->object === 'category') {
$category_ids[] = (int) $item->object_id;
}
}
if (empty($category_ids)) return [];
// Получение категорий
$terms = get_terms([
'taxonomy' => 'category',
'include' => $category_ids,
'hide_empty' => false,
]);
// Индексация по ID
$indexed = [];
foreach ($terms as $term) {
$indexed[$term->term_id] = $term;
}
// Сортировка по порядку в меню
$ordered = [];
foreach ($category_ids as $id) {
if (isset($indexed[$id])) {
$ordered[] = $indexed[$id];
}
}
// Кэшируем результат
wp_cache_set($cache_key, $ordered, $cache_group);
return $ordered;
}
//очищаем кеш при редактировании меню
add_action('wp_update_nav_menu', function($menu_id) {
$menu = wp_get_nav_menu_object($menu_id);
if ($menu && $menu->slug === 'rubrics') {
wp_cache_delete('categories_from_menu_rubrics', 'menu_categories');
}
});

View File

@@ -1,12 +1,7 @@
<?php <?php
$arg_cat = array( // Получаем текущие рубрики из меню
'exclude' => get_expression_parts(), $categories = get_categories_from_menu_exact_order('rubrics');
'order' => 'ASC',
'taxonomy' => 'category',
'hide_empty' => false
);
$categories = get_categories($arg_cat);
$current_term = get_queried_object(); $current_term = get_queried_object();
// Получаем ID текущей категории или категорий поста // Получаем ID текущей категории или категорий поста
@@ -17,9 +12,6 @@ if (is_single()) {
$current_term_ids[] = $current_term->term_id; $current_term_ids[] = $current_term->term_id;
} }
$firstElement = array_shift($categories);
array_push($categories, $firstElement);
foreach ($categories ?? [] as $cat) { foreach ($categories ?? [] as $cat) {
if ($cat->slug === 'uncategorized') continue; if ($cat->slug === 'uncategorized') continue;
$icon = get_field('icon', 'category_' . $cat->term_id); $icon = get_field('icon', 'category_' . $cat->term_id);

View File

@@ -1,14 +1,7 @@
<?php <?php
$categories = get_categories(array(
'exclude' => get_expression_parts(),
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => false
));
// Получаем текущие рубрики из меню
$firstElement = array_shift($categories); $categories = get_categories_from_menu_exact_order('rubrics');
array_push($categories, $firstElement);
foreach ($categories as $category) { foreach ($categories as $category) {
if ($category->slug === 'uncategorized') continue; if ($category->slug === 'uncategorized') continue;

View File

@@ -82,7 +82,12 @@ if (!empty($top_posts)) {
<?} <?}
}
}?> }?>
<div class="most-read__link">
<a href="https://agroexpert.press/all-events/" class="most-read__link-inner text-13 link-icon-after">
Все новости
</a>
</div>
<?}?>

View File

@@ -0,0 +1,59 @@
<?php
// Получаем текущие рубрики из меню
$categories = get_categories_from_menu_exact_order('rubrics');
// Текущая категория или пост
$current_term = get_queried_object();
$current_term_ids = [];
if (is_single()) {
$current_term_ids = wp_get_post_categories($current_term->ID);
} elseif (is_category()) {
$current_term_ids[] = $current_term->term_id;
}
?>
<div class="menu-vertical mobile">
<div class="menu-vertical__head">
<?php if (is_category()) : ?>
<span><?= esc_html($current_term->name); ?></span>
<?php elseif (is_single()) : ?>
<span><?= esc_html(get_cat_name($current_term_ids[0] ?? 0)); ?></span>
<?php else : ?>
<span>Рубрики</span>
<?php endif; ?>
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_373_15802)">
<path d="M7 10L12 15L17 10" stroke="#7C7C7C" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" />
</g>
<defs>
<clipPath id="clip0_373_15802">
<rect width="24" height="24" fill="white" />
</clipPath>
</defs>
</svg>
</div>
<div class="menu-vertical__inner">
<?php foreach ($categories as $cat) : ?>
<?php
if ($cat->slug === 'uncategorized') continue;
$icon = get_field('icon', 'category_' . $cat->term_id);
$bg_image = get_field('bg_image', 'category_' . $cat->term_id);
$class = in_array($cat->term_id, $current_term_ids) ? 'is-active' : '';
?>
<a href="<?= esc_url(get_category_link($cat->term_id)); ?>" class="menu-vertical__item <?= esc_attr($class); ?>">
<span class="menu-vertical__item-icon">
<?php if ($icon): ?>
<img src="<?= esc_url($icon); ?>" alt="" />
<?php endif; ?>
</span>
<span class="menu-vertical__item-text"><?= esc_html($cat->name); ?></span>
</a>
<?php endforeach; ?>
</div>
</div>