0

I've created a custom post type & taxonomy, however when I go to my taxonomy page it throws a 404 error and I can't understand why.

// Register Custom Post Type
function portfolio_post_type() {
    register_taxonomy_for_object_type('category','portfolio');
    register_post_type( 'portfolio',
        array(
            'labels' => array(
            'name' => __('Portfolio'),
                'singular_name' => __('Portfolio'),
                'add_new' => __('Add new item'),
                'add_new_item' => __('Add new item'),
                'edit' => __('Edit'),
                'edit_item' => __('Edit item'),
                'new_item' => __('New item'),
                'view' => __('View item'),
                'view_item' => __('View item'),
                'search_items' => __('Search portfolio'),
                'not_found' => __('No portfolio items found'),
                'not_found_in_trash' => __('No portfolio items found in trash'),
            ),
            'public' => true,
            'rewrite' => array('slug' => 'portfolio'),
            'publicly_queryable' => true,
            'hierarchical' => false,
            'has_archive' => true,
            'menu_icon' => 'dashicons-art',
            'supports' => array('title', 'editor', 'excerpt', 'thumbnail', 'revisions','author'),
            'can_export' => true,
            'taxonomies' => array('portfolio-category'),
        )
    );
}
add_action( 'init', 'portfolio_post_type', 0 );

function portfolio_create_taxonomy() {
    register_taxonomy(
        'portfolio-category',
        'portfolio',
        array(
            'label' => __( 'Category' ),
            'rewrite' => array( 'slug' => 'category' ),
            'hierarchical' => true,
        )
    );
}
add_action( 'init', 'portfolio_create_taxonomy' );

When I click to view a taxonomy it takes me to http://domain.local/category/all/

However it throws the 404 page template.

1

Please replace the "portfolio_create_taxonomy" funcation to below code

function portfolio_create_taxonomy() {


    register_taxonomy(
        'portfolio-category',
        array( 'portfolio' ),

        array(
        'hierarchical'      => true,
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => true,
         'label' => __( 'Category' ),
         'rewrite' => array( 'slug' => 'portfolio-category' ),
         'hierarchical' => true,
        )
    );
}
add_action( 'init', 'portfolio_create_taxonomy' );
1

Slug category is already in use. Reserved_Terms

After making changes, open "Permalink Settings" page and click "Save".

function portfolio_post_type() {
    //register_taxonomy_for_object_type('category','portfolio');
    register_post_type( 'portfolio',
        array(
            'labels' => array(
            'name' => __('Portfolio'),
                'singular_name' => __('Portfolio'),
                'add_new' => __('Add new item'),
                'add_new_item' => __('Add new item'),
                'edit' => __('Edit'),
                'edit_item' => __('Edit item'),
                'new_item' => __('New item'),
                'view' => __('View item'),
                'view_item' => __('View item'),
                'search_items' => __('Search portfolio'),
                'not_found' => __('No portfolio items found'),
                'not_found_in_trash' => __('No portfolio items found in trash'),
            ),
            'public' => true,
            'rewrite' => array('slug' => 'portfolio'),
            'publicly_queryable' => true,
            'hierarchical' => false,
            'has_archive' => true,
            'menu_icon' => 'dashicons-art',
            'supports' => array('title', 'editor', 'excerpt', 'thumbnail', 'revisions','author'),
            'can_export' => true,
            'taxonomies' => array('portfolio-category'),
        )
    );
}
add_action( 'init', 'portfolio_post_type');

function portfolio_create_taxonomy() {
    register_taxonomy(
        'portfolio-category',
        'portfolio',
        array(
            'label' => __( 'Category' ),
            'rewrite' => array( 'slug' => 'portfolio-category' ), // changed from reserved "category" slug
            'hierarchical' => true,
        )
    );
}
add_action( 'init', 'portfolio_create_taxonomy' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.