Files
hantico/wp-content/themes/hantico/inc/ajax.php
T
web_55 39e8d5a8e1 task-17075__opdate-blog (#1)
Сделал
1. Сделать пункты в "ТЗ Статья"
2. Сделать пункты в "ТЗ Хабовая страница"
3. Добавить микроразметку для статьи и блога

https://docs.google.com/document/d/1ouFTKIXGk44hKONDDNWFGXBVqG06LWb7Jt28yFxklME/edit?tab=t.f4g9ytbmj2o8#heading=h.91km96rm8vln

Reviewed-on: #1
Co-authored-by: WEB № 55 - Кирилл | Верстка <web_55@good-production.xyz>
2026-10-26 13:36:52 +03:00

123 lines
3.7 KiB
PHP

<?php
// AJAX-фильтр статей
function theme_filter_articles() {
check_ajax_referer('theme_articles_nonce', 'nonce');
$category = isset($_POST['category']) ? sanitize_title($_POST['category']) : '';
$page = isset($_POST['page']) ? max(1, absint($_POST['page'])) : 1;
$args = [
'post_type' => 'article',
'posts_per_page' => 12,
'paged' => $page,
'orderby' => 'date',
'order' => 'DESC',
];
$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()) {
while ($query->have_posts()) {
$query->the_post();
get_template_part('template-parts/articles-item');
}
wp_reset_postdata();
}
if (!$query->have_posts()) echo '<p>Статьи не найдены.</p>';
$html = ob_get_clean();
wp_send_json_success([
'html' => $html,
'page' => $page,
'max_pages' => $query->max_num_pages,
'category' => $category
]);
}
add_action('wp_ajax_theme_filter_articles', 'theme_filter_articles');
add_action('wp_ajax_nopriv_theme_filter_articles', 'theme_filter_articles');
// AJAX-фильтр вакансий
function theme_filter_vacancies() {
check_ajax_referer('theme_vacancies_nonce', 'nonce');
$category = isset($_POST['category']) ? sanitize_title($_POST['category']) : '';
$page = isset($_POST['page']) ? max(1, absint($_POST['page'])) : 1;
$args = [
'post_type' => 'vacancy',
'posts_per_page' => 12,
'paged' => $page,
'orderby' => 'date',
'order' => 'DESC',
];
if (!empty($category)) {
$args['tax_query'] = [
[
'taxonomy' => 'vacancy_category',
'field' => 'slug',
'terms' => $category,
],
];
}
$query = new WP_Query($args);
ob_start();
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
get_template_part('template-parts/vacancies-item');
}
wp_reset_postdata();
}
$html = ob_get_clean();
wp_send_json_success(
[
'html' => $html,
'page' => $page,
'count' => $query->found_posts,
'max_pages' => $query->max_num_pages,
'category' => $category
]
);
}
add_action('wp_ajax_theme_filter_vacancies', 'theme_filter_vacancies');
add_action('wp_ajax_nopriv_theme_filter_vacancies', 'theme_filter_vacancies');
// AJAX-загрузка постов
function theme_load_more() {
$allowed_types = [
'news' => 'template-parts/news-item',
'review' => 'template-parts/reviews-item',
];
$type = isset($_POST['type']) ? sanitize_key($_POST['type']) : '';
if (!array_key_exists($type, $allowed_types)) {
wp_send_json_error(['message' => 'Invalid type'], 400);
}
check_ajax_referer("theme_{$type}_nonce", 'nonce');
$page = isset($_POST['page']) ? max(1, absint($_POST['page'])) : 1;
$query = new WP_Query([
'post_type' => $type,
'posts_per_page' => 12,
'paged' => $page,
'orderby' => 'date',
'order' => 'DESC',
]);
ob_start();
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
get_template_part($allowed_types[$type]);
}
wp_reset_postdata();
}
$html = ob_get_clean();
wp_send_json_success([
'html' => $html,
'page' => $page,
'max_pages' => $query->max_num_pages,
]);
}
add_action('wp_ajax_theme_load_more', 'theme_load_more');
add_action('wp_ajax_nopriv_theme_load_more', 'theme_load_more');