简体   繁体   中英

Custom Post Type - Category Add Button Not Working

I created a custom post type and also a custom taxonomy for that post type. Everything seems to be working... except when I go to create a new post, if I click on "+ Add New Category" it changes the URL, but nothing happens. All the other JS buttons work and it works just fine on regular posts.. not sure why it is doing this. Below is the codes i am using the the functions.php file.

Register Custom Post Type

/* Custom Post Type Galleries */

function bhgallery_register_post_type() {

    $singular = 'Gallery';
    $plural = 'Galleries';

    $labels = array (
        'name' => $plural,
        'singular' => $singular,
        'add_name' => 'Create New',
        'add_new_item' => 'Create New ' . $singular,
        'edit' => 'Edit',
        'edit_item' => 'Edit ' . $singular,
        'new_item' => 'New' . $singular,
        'view' => 'View' . $singular,
        'view_item' => 'View' . $singular,
        'search_term' => 'Search ' . $plural,
        'parent' => 'Parent ' . $singular,
        'not_found' => 'No ' . $plural . ' Found',
        'not_found_in_trash' => 'No ' . $plural . ' in Trash'

    );

    $args = array ( 
        'labels'                => $labels,
        'public'                => true,
        'public_queryable'      => true,
        'exclude_from_search'   => false,
        'show_in_nav_menus'     => true,
        'show_in_admin_bar'     => true,
        'menu_position'         => 10,
        'menu_icon'             => 'dashicons-camera',
        'can_export'            => true,
        'delete_with_user'      => false,
        'hierarchical'          => false,
        'has_archive'           => true,
        'query_var'             => true,
        'capability_type'       => 'post',
        'map_meta_cap'          => true,
        // 'capabilities' => array();
        'rewrite'               => array(
            'slug'          => 'gallery',
            'with_front'    => true,
            'pages'         => true,
            'feeds'         => false,
        ),
        'supports'              => array(
            'title',
            'thumbnail',
            'editor'
        )
    );

    register_post_type( 'bh_gallery', $args );

}

add_action ( 'init', 'bhgallery_register_post_type');

CUSTOM TAXONOMY

/** Custom Categories for Gallery **/

function bhgallery_register_taxonomy() {

    $plural = 'Categories';
    $singular = 'Category';

    $labels = array (
            'name'                      => $plural,
            'singular_name'             => $singular,
            'search_items'              => 'Search ' . $plural,
            'popular_items'             => 'Popular ' . $plural,
            'all_items'                 => 'All ' . $plural,
            'parent_item'               => null,
            'parent_item_colon'         => null,
            'edit_item'                 => 'Edit ' . $singular,
            'update_item'               => 'Update ' . $singular,
            'add_new_item'              => 'Add New ' . $singular,
            'new_item_name'             => 'New ' . $singular . ' Name',
            'separate_items_with_comas' => 'Seperate ' . $singular . ' with commas',
            'add_or_remove_items'       => 'Add or remove ' . $plural,
            'choose_from_most_used'     => 'Choose from the most used ' . $plural,
            'not_found'                 => 'No ' . $plural . 'fount',
            'menu_name'                 => $plural,
        );

    $args = array (
        'hierarchical'          => true,
        'labels'                => $labels,
        'show_ui'               => true,
        'show_admin_column'     => true,
        'update_count_callback' => '_update_post_term_count',
        'query_var'             => true,
        'rewrite'               => array( 'slug' => 'categories'),
        );

    register_taxonomy( 'gallery category', 'bh_gallery', $args );
}

add_action ( 'init', 'bhgallery_register_taxonomy');

If there is anything else you need to know, let me know.

Your second to last line of code above has

register_taxonomy( 'gallery category', 'bh_gallery', $args );

From the codex, register taxonomy , the first argument should not have any spaces. In fact, there are very few places that spaces are ever allowed and they are mainly limited to strings meant for display.

$taxonomy (string) (required) The name of the taxonomy. Name should only contain lowercase letters and the underscore character, and not be more than 32 characters long (database structure restriction).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM