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

custom post types - Invalid Taxonomy

programmeradmin1浏览0评论

So I registered a custom post type as well as a taxonomy for that post type. I then created a couple items in that taxonomy, but when I go to click on any of them it turns up as Invalid taxonomy.

Here is the code I use to create my custom post type + taxonomy. I have it in a plugin.

class GW_Guides_Post_Type {
    public function __construct() {
        $this->register_post_type();
        $this->metaboxes();
        $this->guides_taxonomy();
    }

    public function guides_taxonomy() {
        register_taxonomy(
            'Guide Categories',
            'gw_guides',
            array(
                'hierarchical' => true,
                'label' => 'Guide Categories')
        );
    }

    public function register_post_type() {
        $args = array(
            'labels' => array(
                'name' => 'Guides',
                'singular_name' => 'Guide',
                'add_new' => 'Add New Guide',
                'add_new_item' => 'Add New Guide',
                'edit_item' => 'Edit Item',
                'new_item' => 'Add New Item',
                'view_item' => 'View News',
                'search_items' => 'Search Guides',
                'not_found' => 'No Guides Found',
                'not_found_in_trash' => 'No Guides Found In Trash'
            ),
            'query_var' => 'guides',
            'rewrite' => array(
                'slug' => 'guides',
            ),
            'public' => true,
            'menu_position' => 5,
            'menu_icon' => admin_url() . 'images/media-button-other.gif',
            'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail' ),
            'has_archive' => true
        );
        register_post_type('gw_guides', $args);
    }   

    public function metaboxes() {
        add_action('add_meta_boxes','add_guides_meta');

        function add_guides_meta() {
            // css id, title, cb funct, page, priority, cb funct arguments
            add_meta_box('gw_guides_meta', 'Featured', 'guides_meta', 'gw_guides');     
        }

        function guides_meta($post) {
            $featured = get_post_meta($post->ID, 'featured', true);
            ?>
                <p>
                    <label for="featured">Featured:</label>
                    <?php 
                        if ($featured == "True") {
                            $checked = 'checked';
                        } else {
                            $checked = '';
                        }
                    ?>
                    <input type="checkbox" name="featured" id="featured" value="True" <?php print($checked) ?> />
                    <?php echo $featured; ?>
                </p>
            <?php
        }

        add_action('save_post', 'save_guides_meta');

        function save_guides_meta($id) {
            update_post_meta(
                $id,
                'featured',
                strip_tags($_POST['featured'])
            );      
        }
    }

}

function add_gw_guides() {
    new GW_Guides_Post_Type();
}

add_action('init', 'add_gw_guides');

?>

So I registered a custom post type as well as a taxonomy for that post type. I then created a couple items in that taxonomy, but when I go to click on any of them it turns up as Invalid taxonomy.

Here is the code I use to create my custom post type + taxonomy. I have it in a plugin.

class GW_Guides_Post_Type {
    public function __construct() {
        $this->register_post_type();
        $this->metaboxes();
        $this->guides_taxonomy();
    }

    public function guides_taxonomy() {
        register_taxonomy(
            'Guide Categories',
            'gw_guides',
            array(
                'hierarchical' => true,
                'label' => 'Guide Categories')
        );
    }

    public function register_post_type() {
        $args = array(
            'labels' => array(
                'name' => 'Guides',
                'singular_name' => 'Guide',
                'add_new' => 'Add New Guide',
                'add_new_item' => 'Add New Guide',
                'edit_item' => 'Edit Item',
                'new_item' => 'Add New Item',
                'view_item' => 'View News',
                'search_items' => 'Search Guides',
                'not_found' => 'No Guides Found',
                'not_found_in_trash' => 'No Guides Found In Trash'
            ),
            'query_var' => 'guides',
            'rewrite' => array(
                'slug' => 'guides',
            ),
            'public' => true,
            'menu_position' => 5,
            'menu_icon' => admin_url() . 'images/media-button-other.gif',
            'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail' ),
            'has_archive' => true
        );
        register_post_type('gw_guides', $args);
    }   

    public function metaboxes() {
        add_action('add_meta_boxes','add_guides_meta');

        function add_guides_meta() {
            // css id, title, cb funct, page, priority, cb funct arguments
            add_meta_box('gw_guides_meta', 'Featured', 'guides_meta', 'gw_guides');     
        }

        function guides_meta($post) {
            $featured = get_post_meta($post->ID, 'featured', true);
            ?>
                <p>
                    <label for="featured">Featured:</label>
                    <?php 
                        if ($featured == "True") {
                            $checked = 'checked';
                        } else {
                            $checked = '';
                        }
                    ?>
                    <input type="checkbox" name="featured" id="featured" value="True" <?php print($checked) ?> />
                    <?php echo $featured; ?>
                </p>
            <?php
        }

        add_action('save_post', 'save_guides_meta');

        function save_guides_meta($id) {
            update_post_meta(
                $id,
                'featured',
                strip_tags($_POST['featured'])
            );      
        }
    }

}

function add_gw_guides() {
    new GW_Guides_Post_Type();
}

add_action('init', 'add_gw_guides');

?>
Share Improve this question edited Aug 25, 2019 at 15:12 Jan Doggen 16911 bronze badges asked Apr 16, 2012 at 23:09 MikeMike 211 silver badge3 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

Your taxonomy has the slug "Guide Categories" which is invalid. The first parameter is the name of the taxonomy, not the human readable name of the taxonomy. That gets defined in the labels.

I recommend you generate your taxonomy registration code using this generator:

http://themergency/generators/wordpress-custom-taxonomy/

It covers all the options available in a nice step by step Wizard with sensible defaults and explanations, and takes a large chunk of the work out of generating a full registration code snippet for you

For bonus points, you can use their custom post type generator too

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论