custom post types - Invalid Taxonomy

So I registered a custom post type as well as a taxonomy for that post type. I then created a couple items in that taxon

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745216935a4617069.html

相关推荐

  • custom post types - Invalid Taxonomy

    So I registered a custom post type as well as a taxonomy for that post type. I then created a couple items in that taxon

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信