• Resolved rebah

    (@rebah)


    Hi there,

    I’ve added CPT ‘blog’ by creating a plugin with the code below. This works. Now I want to add categories, but I can’t seem to make it work. The categories don’t show up in the post editor. I’ve read here and there about show_in_rest and tried to add it, but no succes. Can someone tell me what to do differently? This is the code now:

    function custom_post_type() {
    	$labels = array(
    		'name'  => _x( 'Blog', 'Blogpost General Name', 'text_domain' ),
    		'singular_name'  => _x( 'Blog', 'Blogpost Singular Name', 'text_domain' ),
    		'menu_name'  => __( 'Blog post', 'text_domain' ),
    		'parent_item_colon'  => __( 'Parent Item:', 'text_domain' ),
    		'all_items'  => __( 'Alle posts', 'text_domain' ),
    		'view_item'  => __( 'View Item', 'text_domain' ),
    		'add_new_item'  => __( 'Add New Item', 'text_domain' ),
    		'add_new'  => __( 'Nieuwe post', 'text_domain' ),
    		'edit_item'  => __( 'Edit Item', 'text_domain' ),
    		'update_item'  => __( 'Update Item', 'text_domain' ),
    		'search_items'  => __( 'Search Item', 'text_domain' ),
    		'not_found'  => __( 'Not found', 'text_domain' ),
    		'not_found_in_trash'  => __( 'Not found in Trash', 'text_domain' ),
    	);
    	$args = array(
    		'label'  => __( 'blog', 'text_domain' ),
    		'description'  => __( 'Post Type voor blogs', 'text_domain' ),
    		'labels'  => $labels,
    		'supports'  => array( 'title', 'editor', 'author', 'thumbnail', 'comments', 'custom-fields', ),
    		'taxonomies'  => array( 'gastblog', 'informatief', 'reisverhalen' ),
    		'hierarchical'  => false,
    		'public'  => true,
    		'show_ui'  => true,
    		'show_in_menu'  => true,
    		'show_in_nav_menus'  => true,
    		'show_in_admin_bar'  => true,
    		'menu_position'  => 5,
    		'menu_icon'  => '',
    		'can_export'  => true,
    		'has_archive'  => true,
    		'exclude_from_search' => false,
    		'publicly_queryable'  => true,
    		'capability_type'  => 'page',
    
    	);
    	register_post_type( 'blog', $args );
    }
    // Hook into the 'init' action
    add_action( 'init', 'custom_post_type', 0 );
    • This topic was modified 4 years, 9 months ago by rebah.
    • This topic was modified 4 years, 9 months ago by rebah.
Viewing 9 replies - 1 through 9 (of 9 total)
  • You chose the capability type of page, which seems odd.
    Did you define the taxonomies ‘gastblog’, ‘informatief’, ‘reisverhalen’? You can’t use categories for the new post type because you didn’t put category as one of the taxonomies, so those are just for posts.

    Thread Starter rebah

    (@rebah)

    Hi Joy,

    Thanks for your response. That’s odd indeed. I got the code of a tutorial a while ago. This is not something I do daily. So what would I need to change in order for this to work?

    Do I set the capability type to post? Is that enough or do I need to alter the code some more?

    • This reply was modified 4 years, 9 months ago by rebah.

    The capability type is “The string to use to build the read, edit, and delete capabilities.” The default is ‘post’.
    https://developer.www.ads-software.com/reference/functions/register_post_type/

    If you want to use the standard post category, you would need to list it in the taxonomies parameter. If not, you will need to register your new taxonomies:
    https://developer.www.ads-software.com/reference/functions/register_taxonomy/ or
    https://developer.www.ads-software.com/reference/functions/register_taxonomy_for_object_type/

    Thread Starter rebah

    (@rebah)

    Yes, I think I understood it wrongly. The taxonomies I listed in the code were the categories i wanted to use for the CPT.

    Basically I want to use cat1, cat2, cat3 and cat4 for this new post type. How do I do it?

    I’ve changed the capability type to post and I’ve removed the different taxonomies and made one called ‘subjects’. But I’m not sure what to do next.

    So if you called register_taxonomy to create a taxonomy called subject, then the parameter for show_ui should be set to true, which means it will have the User Interface in the admin menu and the editor.
    Just like the admin menu under Posts shows a link for Categories and Tags, your new post type should have a link under it for Subjects. And when you create a new Blog (new post type), the editor should have a place to enter the Subject taxonomy.

    Thread Starter rebah

    (@rebah)

    To be honest, I’m totally lost. If you have the time and patience, could you tell me which code to use where? This is the code for my plugin now:

    function custom_post_type() {
    	$labels = array(
    		'name'  => _x( 'Blog', 'Blogpost General Name', 'text_domain' ),
    		'singular_name'  => _x( 'Blog', 'Blogpost Singular Name', 'text_domain' ),
    		'menu_name'  => __( 'Blog post', 'text_domain' ),
    		'parent_item_colon'  => __( 'Parent Item:', 'text_domain' ),
    		'all_items'  => __( 'Alle posts', 'text_domain' ),
    		'view_item'  => __( 'View Item', 'text_domain' ),
    		'add_new_item'  => __( 'Add New Item', 'text_domain' ),
    		'add_new'  => __( 'Nieuwe post', 'text_domain' ),
    		'edit_item'  => __( 'Edit Item', 'text_domain' ),
    		'update_item'  => __( 'Update Item', 'text_domain' ),
    		'search_items'  => __( 'Search Item', 'text_domain' ),
    		'not_found'  => __( 'Not found', 'text_domain' ),
    		'not_found_in_trash'  => __( 'Not found in Trash', 'text_domain' ),
    	);
    	$args = array(
    		'label'  => __( 'blog', 'text_domain' ),
    		'description'  => __( 'Post Type voor blogs', 'text_domain' ),
    		'labels'  => $labels,
    		'supports'  => array( 'title', 'editor', 'author', 'thumbnail', 'comments', 'custom-fields', ),
    		'taxonomies'  => array( 'onderwerpen', ),
    		'hierarchical'  => false,
    		'public'  => true,
    		'show_ui'  => true,
    		'show_in_menu'  => true,
    		'show_in_nav_menus'  => true,
    		'show_in_admin_bar'  => true,
    		'menu_position'  => 5,
    		'menu_icon'  => '',
    		'can_export'  => true,
    		'has_archive'  => true,
    		'exclude_from_search' => false,
    		'publicly_queryable'  => true,
    		'capability_type'  => 'post',
    
    	);
    	register_post_type( 'blog', $args );
    }
    // Hook into the 'init' action
    add_action( 'init', 'custom_post_type', 0 );
    ?>

    Is this code correct? What do I still need to add to my functions.php?

    Thread Starter rebah

    (@rebah)

    Yes, i think that may be better. I just wanted to solve it without a plugin. Do you know if there is anything I should think of when switching to the plugin and turning of the one I made with code?

    Thread Starter rebah

    (@rebah)

    Did it with the plugin, thanks for your help!

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Categories to CPT not showing’ is closed to new replies.