☰ Menu
+1 647 493 0594

How to Add Bangla Tags and Categories to a Custom Post Type in WordPress

Posted at March 16, 2020 6:51 am by Shamsia Sharah

Custom post types (CPT) and custom taxonomies offer an effective way to extend WordPress functionality, allowing you to tailor posts, categories, and tags to fit your specific needs. In this tutorial, we will demonstrate how to create custom taxonomies for Bangla tags and categories and associate them with a custom post type. If you’ve already learned how to create a custom post type for Bangla posts, the process for adding custom taxonomies follows a similar approach.

Let’s dive into how you can easily assign Bangla-specific categories and tags to your custom post type.

Step 1: Register Custom Taxonomies

To add custom taxonomies such as categories and tags for your Bangla post type, open your theme’s functions.php file and add the following code:

// Register Custom Taxonomy for Bangla Categories
function custom_taxonomy() {
    $args = array(
        'hierarchical' => true, // Set to true for categories (for tags, set to false)
        'labels' => array(
            'name' => 'Bangla Categories',
            'singular_name' => 'Bangla Category',
            'search_items' => 'Search Bangla Categories',
            'all_items' => 'All Bangla Categories',
            'parent_item' => 'Parent Category',
            'parent_item_colon' => 'Parent Category:',
            'edit_item' => 'Edit Bangla Category',
            'update_item' => 'Update Bangla Category',
            'add_new_item' => 'Add New Bangla Category',
            'new_item_name' => 'New Bangla Category Name',
            'menu_name' => 'Bangla Categories',
        ),
        'show_ui' => true,
        'show_admin_column' => true,
        'query_var' => true,
        'rewrite' => array( 'slug' => 'bangla-category' ),
    );
    register_taxonomy( 'bangla-category', array( 'bangla' ), $args );
}
add_action( 'init', 'custom_taxonomy', 0 );

This code registers a custom taxonomy called Bangla-category for your custom post type bangla. The taxonomy is set to hierarchical, which means it will behave like categories (allowing parent-child relationships). If you prefer non-hierarchical taxonomies like tags, you can set 'hierarchical' => false instead.

Step 2: Assign Custom Taxonomies to Your Custom Post Type

Once the custom taxonomy is registered, you need to link it with your custom post type. This can be done by adding a taxonomies argument to the custom post type registration code. Here’s how to do it:

Locate the code where you registered your custom post type and modify the taxonomies argument as follows:

// Example Custom Post Type registration with custom taxonomies
function create_bangla_post_type() {
    $args = array(
        'labels' => array(
            'name' => 'Bangla Posts',
            'singular_name' => 'Bangla Post',
            'add_new' => 'Add New',
            'add_new_item' => 'Add New Bangla Post',
            'edit_item' => 'Edit Bangla Post',
            'new_item' => 'New Bangla Post',
            'view_item' => 'View Bangla Post',
            'search_items' => 'Search Bangla Posts',
            'not_found' => 'No Bangla Posts found',
        ),
        'public' => true,
        'has_archive' => true,
        'taxonomies' => array( 'bangla-category', 'bangla-tag' ), // Adding custom taxonomies
        'supports' => array( 'title', 'editor', 'thumbnail' ),
        'show_in_rest' => true, // Enable for Gutenberg editor
    );
    register_post_type( 'bangla', $args );
}
add_action( 'init', 'create_bangla_post_type' );

In the code above, we’ve added the taxonomies array to associate both bangla-category and bangla-tag with the bangla custom post type. This ensures that when you create or edit Bangla posts, you can assign them to specific categories and tags that are tailored for Bangla content.

Step 3: Customize Taxonomy Labels for Better User Experience

If you want to personalize the labels for the admin interface, you can tweak the taxonomy’s general name and singular name. For instance, you can change the taxonomy “Category” to “Portfolio” if you’re using a custom taxonomy for a portfolio. Here’s how you can do it:

// Customize Taxonomy Labels for Portfolio
$args = array(
    'hierarchical' => true,
    'labels' => array(
        'name' => 'Portfolios',
        'singular_name' => 'Portfolio',
        'search_items' => 'Search Portfolios',
        'all_items' => 'All Portfolios',
        'parent_item' => 'Parent Portfolio',
        'parent_item_colon' => 'Parent Portfolio:',
        'edit_item' => 'Edit Portfolio',
        'update_item' => 'Update Portfolio',
        'add_new_item' => 'Add New Portfolio',
        'new_item_name' => 'New Portfolio Name',
        'menu_name' => 'Portfolios',
    ),
);

This code replaces the default category label with “Portfolios,” making it more appropriate for a specific content type.

Step 4: Use Hierarchical Taxonomies for Categories

If you want to create hierarchical categories similar to WordPress default categories, you can set 'hierarchical' => true in the taxonomy arguments. This will allow users to create parent and child categories under Bangla posts. If you’re working with tags (non-hierarchical), set 'hierarchical' => false.

// Hierarchical Taxonomy for Categories
$args = array(
    'hierarchical' => true, // Enables parent-child relationships
);

By adjusting the hierarchical setting, you can decide whether the taxonomy behaves like WordPress categories (with parent-child relationships) or like tags (without them).

Step 5: Testing and Fine-Tuning

After adding the custom taxonomy and linking it to your custom post type, test it by creating a new Bangla post and assigning it to one of your custom categories or tags. You should see the taxonomies available in the post editor under the “Categories” and “Tags” sections.

If something isn’t working as expected, double-check the function names, taxonomy slugs, and post type registration to ensure everything is properly set up.


Final Thoughts

By following the steps outlined in this guide, you’ll be able to easily add custom Bangla tags and categories to your WordPress site. These custom taxonomies can be used for any custom post type, such as portfolios, products, recipes, or blogs in different languages.

If you need further assistance, the WordPress Codex and community forums like StackExchange are great resources for learning more about custom post types and taxonomies.