php WordPress自定义分类与CPT相同的Slug

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php WordPress自定义分类与CPT相同的Slug相关的知识,希望对你有一定的参考价值。

Source: http://someweblog.com/wordpress-custom-taxonomy-with-same-slug-as-custom-post-type/
/*
http://someweblog.com/wordpress-custom-taxonomy-with-same-slug-as-custom-post-type/

Custom Post Type: Recipes
Custom Taxonomy: Recipes

Desired URL Structure:

http://domain.com/recipes/
http://domain.com/recipes/chinese/
http://domain.com/recipes/indian/
http://domain.com/recipes/thai/

*/

/*

FUNCTIONS

In functions.php, register the post type and taxonomy.
Note that when registering Custom Taxonomy we use rewrite slug same as the Custom Post Type slug. This is important!

*/

// register custom post type
function create_post_types() {
    register_post_type('recipes', array(
        'labels' => array(
            'name' => 'Recipes',
            'all_items' => 'All Posts'
        ),
        'public' => true
    ));
}
add_action('init', 'create_post_types');
 
// register taxonomy
function create_taxonomies() {
    register_taxonomy('recipes-categories', array('recipes'), array(
        'labels' => array(
            'name' => 'Recipes Categories'
        ),
        'show_ui' => true,
        'show_tagcloud' => false,
        'rewrite' => array('slug' => 'recipes')
    ));
}
add_action('init', 'create_taxonomies');

/*

REWRITE RULES

That would give us the links we wanted. But now, WordPress wouldn’t exactly know what to do with them, cause Custom Types and Taxonomies may not have the same slug, and would give 404 page on those taxonomy terms you created. So, now our function comes into play:
*/

/*
 * Replace Taxonomy slug with Post Type slug in url
 * Version: 1.1
 */
function taxonomy_slug_rewrite($wp_rewrite) {
    $rules = array();
    // get all custom taxonomies
    $taxonomies = get_taxonomies(array('_builtin' => false), 'objects');
    // get all custom post types
    $post_types = get_post_types(array('public' => true, '_builtin' => false), 'objects');
     
    foreach ($post_types as $post_type) {
        foreach ($taxonomies as $taxonomy) {
         
            // go through all post types which this taxonomy is assigned to
            foreach ($taxonomy->object_type as $object_type) {
                 
                // check if taxonomy is registered for this custom type
                if ($object_type == $post_type->rewrite['slug']) {
             
                    // get category objects
                    $terms = get_categories(array('type' => $object_type, 'taxonomy' => $taxonomy->name, 'hide_empty' => 0));
             
                    // make rules
                    foreach ($terms as $term) {
                        $rules[$object_type . '/' . $term->slug . '/?$'] = 'index.php?' . $term->taxonomy . '=' . $term->slug;
                    }
                }
            }
        }
    }
    // merge with global rules
    $wp_rewrite->rules = $rules + $wp_rewrite->rules;
}
add_filter('generate_rewrite_rules', 'taxonomy_slug_rewrite');

/*

WordPress has its own nice rewriting mechanism independent from .htaccess that is processed in php before output. Here we tell WordPress what to do with the URLs we created (only those Taxonomy Terms whose parent Taxonomy has the same slug as its Custom Post Type) and where to redirect them, by adding permalinks to his rewriting object.

*/

/*

PHP FILES

So, now, all you have to do is to create .php files in your theme directory, that will print the content. The filename for the Post Type page is arhchive-{post_type}.php, for the Taxonomy page is taxonomy-{registered_taxonomy}.php, and for the single pages its single-{post_type}.php

In our case from above it would be:

archive-recipes.php
taxonomy-recipes-categories.php
single-recipes.php

*/


/*

PERMALINKS

After doing all this, just go to settings/permalinks and save changes using structure /%postname%/, and thats it!

*/

以上是关于php WordPress自定义分类与CPT相同的Slug的主要内容,如果未能解决你的问题,请参考以下文章

将类别添加到永久链接时,WordPress CPT 会导致 404

从存档调用的 CPT 页面上的 Wordpress 上一个和下一个链接

通过Wordpress循环通过ID循环CPT

php中的动态函数名

markdown Wordpress Dashicons /自定义帖子类型图标/ CPT图标

markdown 资源:Wordpress Dashicons /自定义帖子类型图标/ CPT图标