Answer the question
In order to leave comments, you need to log in
404 error when going to a taxonomy page with a label in Cyrillic, what's wrong?
Hello, I have
a question and need help.
There is a permalink structure for a custom post type:
mysite.com/products/term-1/child-term-2/grandchild-term-3/product-name
<?php
/*
* Plugin Name: MOD
* Description: DEV
*/
/**
* Создание произвольного типа записи
* - register_post_types()
*/
add_action('init', 'register_post_types', 0);
function register_post_types()
{
// тип записи
register_post_type('mod_services', array(
'labels' => array('name' => __('Товары')),
'public' => true,
'show_in_menu' => true,
'show_in_rest' => null,
'rest_base' => null,
'hierarchical' => false,
'supports' => array('title', 'editor', 'thumbnail'),
'has_archive' => true,
'rewrite' => array('slug' => 'catalog', 'with_front' => false),
'query_var' => true,
));
// таксономия
register_taxonomy('mod_services_catalog', array('mod_services'), array(
'labels' => array('name' => __('Категории')),
'public' => true,
'hierarchical' => true,
'rewrite' => array('slug' => 'catalog', 'hierarchical' => true, 'with_front' => false),
'capabilities' => array(),
'meta_box_cb' => null,
'show_admin_column' => false,
'show_in_rest' => null,
'rest_base' => null,
));
}
/**
* Изменяем структуру ссылок записей
* - products_permalink()
*/
add_filter('post_type_link', 'products_permalink', 1, 2);
function products_permalink($permalink, $post)
{
if (strpos($permalink, 'catalog') === false) return $permalink;
$terms = get_the_terms($post, 'mod_services_catalog');
if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) {
$taxonomy_slug = get_term_parents_list($terms[0]->term_id, 'mod_services_catalog', array(
'separator' => '/', 'format' => 'slug', 'link' => false, 'inclusive' => true
));
$taxonomy_slug = trim($taxonomy_slug, '/');
} else {
$taxonomy_slug = 'categories';
}
return str_replace('catalog', 'catalog/' . $taxonomy_slug, $permalink);
}
/**
* Создаём новые правила перезаписи +
* - taxonomy_slug_rewrite()
*/
add_filter('generate_rewrite_rules', 'taxonomy_slug_rewrite');
function taxonomy_slug_rewrite($wp_rewrite)
{
$rules = array();
$taxonomies = get_terms(array(
'taxonomy' => 'mod_services_catalog', 'hide_empty' => false
));
foreach ($taxonomies as $taxonomy) {
$taxonomy_slug = get_term_parents_list($taxonomy->term_id, 'mod_services_catalog', array(
'separator' => '/', 'format' => 'slug', 'link' => false, 'inclusive' => true
));
$rules['^catalog/' . $taxonomy_slug . '?$'] = 'index.php?' . $taxonomy->taxonomy . '=' . $taxonomy->slug;
}
$rules['^catalog/([^/]*)/?$'] = 'index.php?mod_services_catalog=$matches[1]';
$rules['^catalog/(.+?)/page/?([0-9]{1,})/?$'] = 'index.php?mod_services_catalog=$matches[1]&paged=$matches[2]';
$rules['^catalog/(.+?)/([^/]*)/?$'] = 'index.php?mod_services=$matches[2]';
$wp_rewrite->rules = $rules + $wp_rewrite->rules;
}
/**
* Обновляем правила перезаписи при создании/удалении/изменении таксономий
* - hc_reset_rewrite_rules()
*/
add_action('created_mod_services_catalog', 'hc_reset_rewrite_rules');
add_action('delete_mod_services_catalog', 'hc_reset_rewrite_rules');
add_action('edited_mod_services_catalog', 'hc_reset_rewrite_rules');
add_action('save_mod_services_catalog', 'hc_reset_rewrite_rules');
function hc_reset_rewrite_rules()
{
flush_rewrite_rules();
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question