task 17075 | реализовал логику изменения url и кнопки в blog

This commit is contained in:
web_103.2 committed 2026-10-16 16:16:51 +03:00
1 parent 3b38f35be6
commit 9fa68481da
6 files changed
+153 -23

No files matched your search

@@ -6759,3 +6759,27 @@ h4 {
height: 44px;
aspect-ratio: auto;
}
.blog-links{
margin-top: 20px;
display: flex;
justify-content: space-between;
}
.blog-links .btn--transp-red.active{
background: var(--color-red);
color: var(--color-light);
}
@media (max-width: 767.98px) {
.blog-links{
flex-direction: column;
gap: 12px;
}
}
.blog-header__action{
margin-top: 20px;
display: flex;
}
@@ -674,6 +674,7 @@ document.addEventListener('DOMContentLoaded', () => {
currentRequest = new AbortController();
const formData = new FormData();
formData.append('action', 'theme_filter_articles');
formData.append('section', grid.dataset.section || '');
formData.append('nonce', window.themeAjax.nonces.articles);
formData.append('category', category);
formData.append('page', page);
+1
View File
@@ -373,3 +373,4 @@ require_once get_template_directory() . '/inc/acf.php';
require_once get_template_directory() . '/inc/ajax.php';
require_once get_template_directory() . '/inc/menu.php';
require_once get_template_directory() . '/inc/breadcrumbs.php';
require_once get_template_directory() . '/inc/article-urls.php';
+5 -8
View File
@@ -12,15 +12,11 @@ function theme_filter_articles() {
'orderby' => 'date',
'order' => 'DESC',
];
if (!empty($category)) {
$args['tax_query'] = [
[
'taxonomy' => 'article_category',
'field' => 'slug',
'terms' => $category,
]
];
$section = isset($_POST['section']) && is_string($_POST['section']) ? sanitize_key(wp_unslash($_POST['section'])) : '';
if ($section !== '' && !in_array($section, ['general', 'taxi', 'courier', 'cargo'], true)) {
wp_send_json_error(['message' => 'Неизвестный раздел'], 400);
}
$args['tax_query'] = hantico_blog_tax_query($section, $category);
$query = new WP_Query($args);
ob_start();
if ($query->have_posts()) {
@@ -30,6 +26,7 @@ function theme_filter_articles() {
}
wp_reset_postdata();
}
if (!$query->have_posts()) echo '<p>Статьи не найдены.</p>';
$html = ob_get_clean();
wp_send_json_success([
'html' => $html,
@@ -0,0 +1,94 @@
<?php
// Порядок задаёт приоритет, если статье назначено несколько разделов.
function hantico_article_url_base($post_id) {
$sections = [
'taxi' => 'blog-taxi',
'courier' => 'blog-courier',
'cargo' => 'blog-cargo',
];
$terms = get_the_terms($post_id, 'article_chapter');
if (!$terms || is_wp_error($terms)) {
return 'blog';
}
$slugs = wp_list_pluck($terms, 'slug');
foreach ($sections as $slug => $base) {
if (in_array($slug, $slugs, true)) {
return $base;
}
}
return 'blog';
}
add_filter('post_type_link', function ($url, $post, $leavename, $sample) {
if ($post->post_type !== 'article' || (!$post->post_name && !$leavename)) {
return $url;
}
$slug = $leavename ? '%article%' : $post->post_name;
return home_url(user_trailingslashit('/' . hantico_article_url_base($post->ID) . '/' . $slug, 'single'));
}, 10, 4);
add_action('init', function () {
add_rewrite_rule('^blog-(taxi|courier|cargo)/?$', 'index.php?pagename=blog&article_section_filter=$matches[1]', 'top');
// /blog/ продолжает обслуживаться существующими правилами типа article.
add_rewrite_rule(
'^(?:blog-taxi|blog-courier|blog-cargo)/([^/]+)/?$',
'index.php?post_type=article&name=$matches[1]',
'top'
);
});
add_filter('query_vars', function ($vars) {
$vars[] = 'article_section_filter';
return $vars;
});
function hantico_blog_section() {
$section = get_query_var('article_section_filter', '');
if (!$section && isset($_GET['section']) && is_string($_GET['section'])) {
$section = sanitize_key(wp_unslash($_GET['section']));
}
return in_array($section, ['general', 'taxi', 'courier', 'cargo'], true) ? $section : '';
}
function hantico_blog_tax_query($section, $category = '') {
$query = ['relation' => 'AND'];
if ($section !== '') {
$query[] = ['taxonomy' => 'article_chapter', 'field' => 'slug', 'terms' => $section];
}
if ($category !== '') {
$query[] = ['taxonomy' => 'article_category', 'field' => 'slug', 'terms' => $category];
}
return count($query) > 1 ? $query : [];
}
add_filter('redirect_canonical', function ($redirect) {
return is_page('blog') && get_query_var('article_section_filter') ? false : $redirect;
});
// Старый адрес и адрес с чужим разделом ведут на текущую ссылку статьи.
add_action('template_redirect', function () {
if (!is_singular('article') || is_preview() || is_feed() || is_embed()
|| !in_array($_SERVER['REQUEST_METHOD'] ?? '', ['GET', 'HEAD'], true)) {
return;
}
$request_path = wp_parse_url(wp_unslash($_SERVER['REQUEST_URI'] ?? ''), PHP_URL_PATH);
$home_path = wp_parse_url(home_url('/'), PHP_URL_PATH);
$relative_path = substr((string) $request_path, strlen((string) $home_path));
if (!preg_match('~^blog(?:-taxi|-courier|-cargo)?/[^/]+/?$~', $relative_path)) {
return;
}
$url = get_permalink(get_queried_object_id());
$canonical_path = wp_parse_url($url, PHP_URL_PATH);
if (rawurldecode((string) $request_path) === rawurldecode((string) $canonical_path)) {
return;
}
// Сохраняем параметры, например UTM-метки.
$query = wp_parse_url(wp_unslash($_SERVER['REQUEST_URI'] ?? ''), PHP_URL_QUERY);
if ($query) {
$url .= '?' . $query;
}
if (wp_safe_redirect($url, 301)) {
exit;
}
}, 5);
@@ -10,6 +10,14 @@ Template Name: Блог
<main class="main">
<?php
$current_section = hantico_blog_section();
$section_urls = [
'' => home_url('/blog/'),
'general' => add_query_arg('section', 'general', home_url('/blog/')),
'taxi' => home_url('/blog-taxi/'),
'courier' => home_url('/blog-courier/'),
'cargo' => home_url('/blog-cargo/'),
];
$blog_title = get_field('blog_title');
$blog_descr = get_field('blog_descr');
$blog_args = [
@@ -18,6 +26,7 @@ Template Name: Блог
'orderby' => 'date',
'order' => 'DESC'
];
$blog_args['tax_query'] = hantico_blog_tax_query($current_section);
$blog_query = new WP_Query($blog_args);
if ($blog_query->have_posts()) :
?>
@@ -32,8 +41,24 @@ Template Name: Блог
<div class="blog-logo">
<img src="<?php echo get_theme_file_uri('assets/img/icons/logo2.svg'); ?>" alt="Hantico" width="112" height="147">
</div>
<div class="blog-header__action">
<a href="/about/" class="blog-item__read-btn btn btn--md btn--transp-red ">О компании</a>
</div>
</div>
<?php endif; ?>
<?php
if (!is_wp_error($terms)) :
$blog_url = $section_urls[$current_section];
?>
<div class="blog-links">
<?php foreach (['taxi' => 'Таксисты', 'courier' => 'Курьеры', 'cargo' => 'Грузоперевозчики'] as $section => $label) : ?>
<a href="<?php echo esc_url($section_urls[$section]); ?>" class="blog-item__read-btn btn btn--md btn--transp-red <?php if ($current_section === (string) $section) echo ' active'; ?>"<?php if ($current_section === (string) $section) echo ' aria-current="page"'; ?>><?php echo esc_html($label); ?></a>
<?php endforeach; ?>
</div>
<div class="blog-slider">
<div class="blog-slider__container swiper swiper-fade">
<div class="blog-slider__wrapper swiper-wrapper">
@@ -91,25 +116,12 @@ Template Name: Блог
'orderby' => 'date',
'order' => 'DESC'
];
if (!empty($current_category)) {
$blog_args['tax_query'] = [
[
'taxonomy' => 'article_category',
'field' => 'slug',
'terms' => $current_category
],
];
}
$blog_args['tax_query'] = hantico_blog_tax_query($current_section, $current_category);
$blog_query = new WP_Query($blog_args);
if ($blog_query->have_posts()) :
?>
<section class="articles articles--page">
<div class="container">
<h2 class="articles-title articles-title--sm">Все статьи</h2>
<?php
if ($terms) :
$blog_url = get_permalink();
?>
<nav class="articles-filter">
<ul class="articles-filter__list">
<li class="articles-filter__item<?php if (!$current_category) echo ' is-active'; ?>">
@@ -130,10 +142,12 @@ Template Name: Блог
<div
class="articles-grid"
data-page="1"
data-section="<?php echo esc_attr($current_section); ?>"
data-max-pages="<?php echo $blog_query->max_num_pages; ?>"
data-category="<?php echo $current_category; ?>"
>
<?php
if (!$blog_query->have_posts()) echo '<p>Статьи не найдены.</p>';
while ($blog_query->have_posts()) {
$blog_query->the_post();
get_template_part('template-parts/articles-item');
@@ -145,7 +159,6 @@ Template Name: Блог
</div>
</div>
</section>
<?php endif; ?>
<?php get_template_part('template-parts/socials'); ?>
<?php get_template_part('template-parts/getapp'); ?>