add update rubrics

This commit is contained in:
argoexpert press
2024-05-25 16:51:55 +03:00
parent 5a9c8597ba
commit 641399598f
6 changed files with 113 additions and 5 deletions

View File

@@ -8,7 +8,7 @@
$args = array( $args = array(
'post_type' => 'post', // Или ваш пользовательский тип постов, если необходимо 'post_type' => 'post', // Или ваш пользовательский тип постов, если необходимо
'post__not_in' => [ $mainpost_id ], 'post__not_in' => [ $mainpost_id ],
'posts_per_page' => 10, // Количество постов на странице 'posts_per_page' => 20, // Количество постов на странице
'orderby' => 'date', // сортировка по дате 'orderby' => 'date', // сортировка по дате
'order' => 'DESC', // по убыванию (сначала новые) 'order' => 'DESC', // по убыванию (сначала новые)
'ignore_sticky_posts' => 1, 'ignore_sticky_posts' => 1,
@@ -18,7 +18,7 @@
?> ?>
<?php get_template_part('en/post-list-en', null, ['large_first_image' => false, 'query' => $query]); ?> <?php get_template_part('en/post-list-en', null, ['large_first_image' => false, 'query' => $query]); ?>
</div> </div>
<button class="articles-preview__show-next">More Read</button> <!--<button class="articles-preview__show-next">More Read</button>-->
</div> </div>
<?php get_footer( 'en' ); ?> <?php get_footer( 'en' ); ?>

View File

@@ -11,7 +11,7 @@
$paged = (get_query_var('paged')) ? absint(get_query_var('paged')) : 1; $paged = (get_query_var('paged')) ? absint(get_query_var('paged')) : 1;
$args = array( $args = array(
'post_type' => 'post', 'post_type' => 'post',
'posts_per_page' => 5, 'posts_per_page' => 9,
'paged' => $paged, 'paged' => $paged,
'category__in' => get_queried_object_id() 'category__in' => get_queried_object_id()
); );

10
en/menu-primary-en.php Normal file
View File

@@ -0,0 +1,10 @@
<?php
$menu_id = 761;
$menu_items = wp_get_nav_menu_items($menu_id);
?>
<?php foreach ($menu_items ?: [] as $menu_item):?>
<li class="menu-item">
<a href="<?=esc_url($menu_item->url);?>"><?=$menu_item->title?></a>
</li>
<?php endforeach ?>

View File

@@ -960,3 +960,99 @@ function use_custom_template_for_en_subcategories($template) {
add_filter('template_include', 'use_custom_template_for_en_subcategories'); add_filter('template_include', 'use_custom_template_for_en_subcategories');
/**
* update rubrics
*/
function add_posts_to_category_744() {
// Получаем ID рубрики 740 и всех ее подрубрик
$parent_category_id = 740;
$subcategories = get_categories(array(
'child_of' => $parent_category_id,
'hide_empty' => false
));
// Массив ID всех подрубрик 740
$subcategory_ids = wp_list_pluck($subcategories, 'term_id');
$all_category_ids = array_merge(array($parent_category_id), $subcategory_ids);
// Запрос для получения постов, которые имеют рубрику 740 или любую из ее подрубрик
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => $all_category_ids,
'operator' => 'IN'
),
),
);
// Получаем все посты с указанной рубрикой или ее подрубриками
$query = new WP_Query($args);
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
// Получаем ID текущего поста
$post_id = get_the_ID();
// Получаем все рубрики текущего поста
$post_categories = wp_get_post_categories($post_id);
// Проверяем, есть ли у поста только рубрика 740
if (count($post_categories) == 1 && in_array($parent_category_id, $post_categories)) {
// Добавляем пост в рубрику с ID 744
wp_set_post_categories($post_id, array_merge($post_categories, array(744)));
} else {
// Проверяем, если у поста есть подрубрика 740, но нет основной рубрики 740
$has_subcategory = array_intersect($post_categories, $subcategory_ids);
if ($has_subcategory && !in_array($parent_category_id, $post_categories)) {
// Добавляем основную рубрику 740
wp_set_post_categories($post_id, array_merge($post_categories, array($parent_category_id)));
}
}
}
// Сбрасываем пост данные
wp_reset_postdata();
}
}
function add_custom_admin_menu() {
add_menu_page(
'Update Posts Categories', // Заголовок страницы
'Update Categories EN', // Текст меню
'manage_options', // Возможность доступа (только администраторы)
'update-categories', // Уникальный идентификатор страницы
'update_categories_callback' // Функция для отображения контента страницы
);
}
add_action('admin_menu', 'add_custom_admin_menu');
function update_categories_callback() {
if (isset($_POST['update_categories'])) {
add_posts_to_category_744();
echo '<div class="updated"><p>Posts have been updated successfully.</p></div>';
}
?>
<div class="wrap">
<h1>Update Posts Categories</h1>
<form method="post">
<input type="hidden" name="update_categories" value="1">
<p>
<input type="submit" class="button-primary" value="Update Categories">
</p>
</form>
</div>
<?php
}

View File

@@ -59,6 +59,7 @@ height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<nav class="nav header__nav"> <nav class="nav header__nav">
<?php get_template_part('en/mainmenu-en'); ?> <?php get_template_part('en/mainmenu-en'); ?>
</nav> </nav>
<div class="lang-switch"><a href="<?php echo home_url(); ?>">RU</a></div>
<div class="header__right"> <div class="header__right">
<div class="header__actions"> <div class="header__actions">
<a href="/search/"> <a href="/search/">
@@ -102,7 +103,7 @@ height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
</div> </div>
<div class="header-mobile"> <div class="header-mobile">
<nav class="nav header-mobile__nav"> <nav class="nav header-mobile__nav">
<?php get_template_part('partials/menu-primary'); ?> <?php get_template_part('en/menu-primary-en'); ?>
</nav> </nav>
<div class="socials header-mobile__socials"> <div class="socials header-mobile__socials">
<?php get_template_part('partials/menu-socials'); ?> <?php get_template_part('partials/menu-socials'); ?>

View File

@@ -72,6 +72,7 @@ height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<nav class="nav header__nav"> <nav class="nav header__nav">
<?php get_template_part('partials/menu-primary'); ?> <?php get_template_part('partials/menu-primary'); ?>
</nav> </nav>
<div class="header__right"> <div class="header__right">
<div class="header__actions"> <div class="header__actions">
<a href="/search/"> <a href="/search/">