diff --git a/wp-content/themes/hantico/assets/css/style.css b/wp-content/themes/hantico/assets/css/style.css index 48dd6a6..dbae473 100644 --- a/wp-content/themes/hantico/assets/css/style.css +++ b/wp-content/themes/hantico/assets/css/style.css @@ -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; +} \ No newline at end of file diff --git a/wp-content/themes/hantico/assets/js/scripts.js b/wp-content/themes/hantico/assets/js/scripts.js index 2e01e47..a348e4c 100644 --- a/wp-content/themes/hantico/assets/js/scripts.js +++ b/wp-content/themes/hantico/assets/js/scripts.js @@ -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); diff --git a/wp-content/themes/hantico/functions.php b/wp-content/themes/hantico/functions.php index 4442bb9..30205d0 100644 --- a/wp-content/themes/hantico/functions.php +++ b/wp-content/themes/hantico/functions.php @@ -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'; diff --git a/wp-content/themes/hantico/inc/ajax.php b/wp-content/themes/hantico/inc/ajax.php index 780123c..90c062d 100644 --- a/wp-content/themes/hantico/inc/ajax.php +++ b/wp-content/themes/hantico/inc/ajax.php @@ -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 '
Статьи не найдены.
'; $html = ob_get_clean(); wp_send_json_success([ 'html' => $html, diff --git a/wp-content/themes/hantico/inc/article-urls.php b/wp-content/themes/hantico/inc/article-urls.php new file mode 100644 index 0000000..f2e1653 --- /dev/null +++ b/wp-content/themes/hantico/inc/article-urls.php @@ -0,0 +1,94 @@ + '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); diff --git a/wp-content/themes/hantico/templates/template-blog.php b/wp-content/themes/hantico/templates/template-blog.php index be3f7f1..0969559 100644 --- a/wp-content/themes/hantico/templates/template-blog.php +++ b/wp-content/themes/hantico/templates/template-blog.php @@ -10,6 +10,14 @@ Template Name: Блог