Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
396 views
in Technique[技术] by (71.8m points)

url rewriting - Wordpress permalink structure with custom taxonomy and without base-name

I want to create the following permalink structure

custom post type: resources
custom taxonomy: resources-category (e.g. Sales, Business, ...)

how can I get this structure: url.com/%resources-category%/detail-slug

e.g.
url.com/sales/how-to-sale
url.com/business/how-to-something

What I get until now is, that I can open this urls, but they are automatically redirected to my custom post type resources slug
e.g. url.com/sales/how-to-sale redirects to url.com/resources/how-to-sale

I registered the post type:


    function registerTypeResources () {
        register_post_type('resources',
            array(
                'labels' => array(
                    'name' => __('Resources'),
                    'singular_name' => __('Resources')
                ),
                'public' => true,
                'has_archive' => false,
                'supports' => array('title', 'thumbnail', 'editor'),
                'show_in_rest' => true,
            )
        );
    }

    add_action('init', 'registerTypeResources');

I've added the taxonomy:


    add_action('init', 'registerRessourcesTaxonomy', 0);
    
    function registerRessourcesTaxonomy () {
        register_taxonomy(
            'resources-category',
            'resources',
            array(
                'labels' => array(
                    'name' => __('Categories'),
                    'add_new_item' => __('Add new category'),
                    'new_item_name' => __('New category ')
                ),
                'show_in_rest' => true,
                'show_ui' => true,
                'show_tagcloud' => false,
                'hierarchical' => true
            )
        );
    
    }

I added a rewrite:


    add_action('init', function () {
        add_rewrite_rule('sales/(.+)/?$', 'index.php?resources=$matches[1]', 'bottom');
        add_rewrite_rule('business/(.+)/?$', 'index.php?resources=$matches[1]', 'bottom');
    });

How can I disable the redirect to resources?

Thanks for your help!

question from:https://stackoverflow.com/questions/65843800/wordpress-permalink-structure-with-custom-taxonomy-and-without-base-name

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I think I found the answer for my self. Maybe someone else needs this solution, too.

https://wordpress.stackexchange.com/questions/328279/custom-permalink-structure-custom-taxonomy-post-name-without-cpt-slug


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...