最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

custom taxonomy - How do I get the correct URL?

programmeradmin2浏览0评论

Helo, WordPress Community! Help me, please.

Created a custom post type with one taxonomy added as a category. Overwrite rules have also been created to display the complete structure of the CNC, for example: home/post-type/term-parent/term-child/post-name

I am trying to add a second taxonomy like tags, but whatever I do, I get a 404 error or the link structure breaks.

Can someone help me with this?

<?php
/*
* Plugin Name: MOD
* Description: DEV
*/


/**
 * mod_cpt_services() -------------------- Создание произвольного типа записи.
 *
 * * cpt_services ------------------------ Регистрация типа записи.
 * * cpt_services_cat -------------------- Регистрация таксономии, как категорий.
 * * cpt_services_tag -------------------- Регистрация таксономии, как тега.
 *
 * permalink_services_cat() -------------- Структура ссылок для категорий.
 * rewrite_rules_services_cat() ---------- Генерация правил перезаписи ссылок для категорий.
 *
 * permalink_services_tag() -------------- Структура ссылок для тегов.
 * rewrite_rules_services_tag() ---------- Генерация правил перезаписи ссылок для тегов.
 *
 * reset_rewrite_rules() ----------------- Сброс правил перезаписи при создании/удалении/изменении категорий и тегов.
 */


# mod_cpt_services()
# 1
add_action('init', 'mod_cpt_services', 0);
function mod_cpt_services()
{
    # cpt_services
    register_post_type('cpt_services', array(
        'labels'        => array('name' => 'Услуги',),
        'public'        => true,
        'menu_position' => 100,
        'menu_icon'     => 'dashicons-category',
        'rewrite'       => array('slug' => 'services', 'with_front' => false),
        'supports'      => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'page-attributes', 'post-formats'),
        'query_var'     => true,
        'show_in_rest'  => true,
    ));
    # cpt_services_cat
    register_taxonomy('cpt_services_cat', array('cpt_services'), array(
        'labels'        => array( 'name' => 'Категории услуг',),
        'public'        => true,
        'hierarchical'  => true,
        'rewrite'       => array('slug' => 'services', 'hierarchical' => true, 'with_front' => false),
        'query_var'     => true,
        'show_in_rest'  => true,
    ));
    # cpt_services_tag
    register_taxonomy('cpt_services_tag', array('cpt_services'), array(
        'labels'        => array( 'name' => 'Теги услуг',),
        'public'        => true,
        'hierarchical'  => false,
        'rewrite'       => array('slug' => 'services', 'hierarchical' => true, 'with_front' => false),
        'query_var'     => true,
        'show_in_rest'  => true,
    ));
    flush_rewrite_rules();    
}

# permalink_services_cat()
# 2
add_filter('post_type_link', 'permalink_services_cat', 1, 2);
function permalink_services_cat($permalink, $post)
{
    if (strpos($permalink, 'services') === false) return $permalink;
    $terms = get_the_terms($post, 'cpt_services_cat');
    if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) {
        $taxonomy_slug = get_term_parents_list($terms[0]->term_id, 'cpt_services_cat', array(
            'separator' => '/', 'format' => 'slug', 'link' => false, 'inclusive' => true
        ));
        $taxonomy_slug = trim($taxonomy_slug, '/');
    } else {
        $taxonomy_slug = 'sandbox';
    }
    return str_replace('services', 'services/' . $taxonomy_slug, $permalink);
}

# rewrite_rules_services_cat()
# 3
add_filter('generate_rewrite_rules', 'rewrite_rules_services_cat');
function rewrite_rules_services_cat($wp_rewrite)
{
    $rules = array();
    $taxonomies = get_terms(array(
        'taxonomy' => 'cpt_services_cat', 'hide_empty' => false
    ));
    foreach ($taxonomies as $taxonomy) {
        $taxonomy_slug = get_term_parents_list($taxonomy->term_id, 'cpt_services_cat', array(
            'separator' => '/', 'format' => 'slug', 'link' => false, 'inclusive' => true
        ));
        $rules['^services/' . $taxonomy_slug . '?$'] = 'index.php?' . $taxonomy->taxonomy . '=' . $taxonomy->slug;
    }
    $rules['^services/([^/]*)/?$'] = 'index.php?cpt_services_cat=$matches[1]';
    $rules['^services/(.+?)/page/?([0-9]{1,})/?$'] = 'index.php?cpt_services_cat=$matches[1]&paged=$matches[2]';
    $rules['^services/(.+?)/([^/]*)/?$'] = 'index.php?cpt_services=$matches[2]';
    $wp_rewrite->rules = $rules + $wp_rewrite->rules;
}

/*
# permalink_services_tag()
# 4
add_filter('post_type_link', 'permalink_services_tag', 1, 2);
function permalink_services_tag($permalink, $post)
{
    if (strpos($permalink, 'services') === false) return $permalink;
    $terms = get_the_terms($post, 'cpt_services_tag');
    if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) {
        $taxonomy_slug = get_term_parents_list($terms[0]->term_id, 'cpt_services_tag', array(
            'separator' => '/', 'format' => 'slug', 'link' => false, 'inclusive' => true
        ));
        $taxonomy_slug = trim($taxonomy_slug, '/');
    } else {
        $taxonomy_slug = 'sandbox';
    }
    return str_replace('services', 'services/' . $taxonomy_slug, $permalink);
}

# rewrite_rules_services_tag()
# 5
add_filter('generate_rewrite_rules', 'rewrite_rules_services_tag');
function rewrite_rules_services_tag($wp_rewrite)
{
    $rules = array();
    $taxonomies = get_terms(array(
        'taxonomy' => 'cpt_services_tag', 'hide_empty' => false
    ));
    foreach ($taxonomies as $taxonomy) {
        $taxonomy_slug = get_term_parents_list($taxonomy->term_id, 'cpt_services_tag', array(
            'separator' => '/', 'format' => 'slug', 'link' => false, 'inclusive' => true
        ));
        $rules['^services/' . $taxonomy_slug . '?$'] = 'index.php?' . $taxonomy->taxonomy . '=' . $taxonomy->slug;
    }
    $rules['^services/([^/]*)/?$'] = 'index.php?cpt_services_tag=$matches[1]';
    $rules['^services/(.+?)/page/?([0-9]{1,})/?$'] = 'index.php?cpt_services_tag=$matches[1]&paged=$matches[2]';
    $rules['^services/(.+?)/([^/]*)/?$'] = 'index.php?cpt_services=$matches[2]';
    $wp_rewrite->rules = $rules + $wp_rewrite->rules;
}
*/

# reset_rewrite_rules()
# 6
# cpt_services_cat
add_action('created_cpt_services_cat', 'reset_rewrite_rules');
add_action('delete_cpt_services_cat', 'reset_rewrite_rules');
add_action('edited_cpt_services_cat', 'reset_rewrite_rules');
# cpt_services_tag
add_action('created_cpt_services_tag', 'reset_rewrite_rules');
add_action('delete_cpt_services_tag', 'reset_rewrite_rules');
add_action('edited_cpt_services_tag', 'reset_rewrite_rules');
function reset_rewrite_rules()
{
    flush_rewrite_rules();
}

I apologize for my english. I am writing to you via google translator.

Glad for any help. Other solutions are also welcome.

发布评论

评论列表(0)

  1. 暂无评论