Monday, November 30, 2020
Home Blogging How to Add Categories to a Custom Post Type in WordPress

How to Add Categories to a Custom Post Type in WordPress

Lately one in every of our person requested us if it was potential so as to add categories to a {custom} put up sort they’ve created. Categories are one of many built-in taxonomies in WordPress. By default they seem just for posts. Nevertheless, in some eventualities you could need to share them with a {custom} put up sort as properly. On this article, we’ll present you learn how to add categories to a {custom} put up sort in WordPress. We may even present you learn how to show a number of put up sorts in your category archive web page.

The Plugin Technique

For our newbie stage customers, we advocate utilizing Customized Publish Kind UI plugin to create {custom} put up sorts. When utilizing Customized Publish Kind UI plugin, you may have the choice to affiliate your {custom} put up sort to any built-in or {custom} taxonomy together with categories.

First you might want to set up and activate the Customized Publish Kind UI plugin. For extra particulars, see our step-by-step information on learn how to set up a WordPress plugin.

Upon set up, you might want to go to CPT UI » Add/Edit Publish Sorts to create a brand new {custom} put up sort or edit an present {custom} put up sort you created with the plugin.

Scroll down on the Superior Choices to the underside and there you will note the In-built Taxnomies possibility. Verify the field subsequent to categories and save your {custom} put up sort.

Don’t neglect to click on on the save put up sort button to retailer your settings.

Manually Including Categories to a Customized Publish Kind

In the event you created your {custom} put up sort by including the code in your theme’s capabilities.php file or a site-specific plugin, then you’ll have to modify the code so as to add class as supported taxonomy.

All you might want to do is add this line within the arguments in your CPT.

1
'taxonomies'  => array( 'category' ),

It’s possible that you could be have already got this line within the present code in your CPT with another {custom} taxonomy in it. In the event you do, then you definitely simply want so as to add a comma after that and add class, like this:

1
'taxonomies'          => array('topics', 'category' ),

Here’s a full instance code the place we’ve got created a {custom} put up sort referred to as motion pictures with assist for built-in categories.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
function custom_post_type() {
// Set UI labels for Custom Post Type
    $labels = array(
        'name'                => _x( 'Movies', 'Post Type General Name', 'twentythirteen' ),
        'singular_name'       => _x( 'Movie', 'Post Type Singular Name', 'twentythirteen' ),
        'menu_name'           => __( 'Movies', 'twentythirteen' ),
        'parent_item_colon'   => __( 'Parent Movie', 'twentythirteen' ),
        'all_items'           => __( 'All Movies', 'twentythirteen' ),
        'view_item'           => __( 'View Movie', 'twentythirteen' ),
        'add_new_item'        => __( 'Add New Movie', 'twentythirteen' ),
        'add_new'             => __( 'Add New', 'twentythirteen' ),
        'edit_item'           => __( 'Edit Movie', 'twentythirteen' ),
        'update_item'         => __( 'Update Movie', 'twentythirteen' ),
        'search_items'        => __( 'Search Movie', 'twentythirteen' ),
        'not_found'           => __( 'Not Found', 'twentythirteen' ),
        'not_found_in_trash'  => __( 'Not found in Trash', 'twentythirteen' ),
    );
    
// Set other options for Custom Post Type
    
    $args = array(
        'label'               => __( 'movies', 'twentythirteen' ),
        'description'         => __( 'Movie news and reviews', 'twentythirteen' ),
        'labels'              => $labels,
        'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
        'hierarchical'        => false,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 5,
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'page',
        
        // This is where we add taxonomies to our CPT
        'taxonomies'          => array( 'category' ),
    );
    
    // Registering your Custom Post Type
    register_post_type( 'movies', $args );
}
/* Hook into the 'init' action so that the function
* Containing our post type registration is not
* unnecessarily executed.
*/
add_action( 'init', 'custom_post_type', 0 );

Displaying A number of Publish Sorts on Class Web page

By default the category pages in your WordPress website will solely show the default ‘Posts’ put up sort. To show your {custom} put up sorts on the identical category web page as your default posts, you might want to add this code into your theme’s capabilities.php or a site-specific plugin.

1
2
3
4
5
6
7
8
9
10
11
12
add_filter('pre_get_posts', 'query_post_type');
function query_post_type($query) {
  if( is_category() ) {
    $post_type = get_query_var('post_type');
    if($post_type)
        $post_type = $post_type;
    else
        $post_type = array('nav_menu_item', 'post', 'movies'); // don't forget nav_menu_item to allow menus to work!
    $query->set('post_type',$post_type);
    return $query;
    }
}

Don’t neglect to switch motion pictures with the title of your personal {custom} put up sort.

You May Also Like : How to Move GoDaddy Website Builder Site to WordPress

That’s all, we hope this text helped you add categories to your {custom} put up sort in WordPress. You need to use the identical strategies so as to add tags to your {custom} put up sorts as properly.

If you liked this article, make sure to hit the bell for getting our latest updates and share this article with your friends in every social media platforms.

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -

Most Popular

How to Signup for Godaddy Affiliate Program & Start Making Money

GoDaddy is likely one of the well-liked model names out there of area and web-hosting. They are well-liked for a numerous cause, and so they...

How To Start Selling Website on Flippa: A Beginner’s Guide To Success

If you may have been working on-line for lengthy and into Website flipping, Flippa is one identify that you need to have heard of. Flippa is a...

How to Fix Your Connection is Not Private Error (Site Owners Guide)

Are you seeing ‘Your connection is not private’ error in your WordPress web site? This is the error message you’ll see on Google Chrome. The...

How to Fix the WordPress White Page of Death (Step by Step)

The WordPress white page of dying is one of the most widespread WordPress errors. It can also be one of the most irritating ones as...

Recent Comments