How to create a custom post type for Bangla Blog Section

From the beginning of this blog, I am always thinking to create a separate section for Bangla to display the Bengali version of my blog. I was trying many methods, e.g., custom directory,  subdomains, and WordPress default multi-site option but none of them fully satisfy me. Because the previous methods are just extended my workloads. For example, I can’t use the same image from single upload and have trouble managing the multi-site, etc.

So, the last method that I use is creating a custom post type that allows me to use the same image from single upload and developing Bengali posts from the same dashboard. It reduces my workload, and I can do a blog from a single site, single login, single image upload and attach them to my different post types, One is for Bangla, and the other is for English.

If you are thinking to create a different version of your blog for Bangla or your native language, custom post type can be a great solution. For the first time, it is not easier for me to create a different section for Bangla until I use the wp generate tool. Here’s how you can create a custom post type for Bangla or your native language.

Step-1: Open your functions.php file in a text editor or simply in wp default editor and add the following line:

function custom_post_type_bangla() {

    $labels = array(
        'name'                => _x( 'Bangla Posts', 'Post Type General Name', 'text_domain' ),
        'singular_name'       => _x( 'Bangla Post', 'Post Type Singular Name', 'text_domain' ),
        'menu_name'           => __( 'Bangla Posts', 'text_domain' ),
        'name_admin_bar'      => __( 'Bangla Posts', 'text_domain' ),
        'parent_item_colon'   => __( 'Parent Post:', 'text_domain' ),
        'all_items'           => __( 'All Posts', 'text_domain' ),
        'add_new_item'        => __( 'Add New Post', 'text_domain' ),
        'add_new'             => __( 'Add New', 'text_domain' ),
        'new_item'            => __( 'New Post', 'text_domain' ),
        'edit_item'           => __( 'Edit Post', 'text_domain' ),
        'update_item'         => __( 'Update Post', 'text_domain' ),
        'view_item'           => __( 'View Post', 'text_domain' ),
        'search_items'        => __( 'Search Post', 'text_domain' ),
        'not_found'           => __( 'Not found', 'text_domain' ),
        'not_found_in_trash'  => __( 'Not found in Trash', 'text_domain' ),
    );
    $args = array(
        'label'               => __( 'Bangla Post', 'text_domain' ),
        'description'         => __( 'Bangla Post', 'text_domain' ),
        'labels'              => $labels,
        'supports'            => array( 'title','editor','comments','thumbnail','excerpt','author','page-attributes','post-formats','revisions','custom-fields' ),
        'taxonomies'          => array( 'category', 'post-tag' ),
        'hierarchical'        => false,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'menu_position'       => 5,
        'show_in_admin_bar'   => true,
        'show_in_nav_menus'   => true,
        'can_export'          => true,
        'has_archive'         => true,        
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'page',
    );
    register_post_type( 'bangla', $args );

}
add_action( 'init', 'custom_post_type_bangla', 0 );

Now, Go to the Settings >  Permalinks and save the changes. Create a post from custom post type and view the post.

If, everything is fine then no problem. Otherwise, you can create a single and archive template to generate a custom post type’s post. For Bangla post type, your single and archive template name should be like this: single-bangla.php or archive-bangla.php. Always remember that custom post type templates always include its name as a slug.

Step-2: Displaying the custom post type with the regular post: It is a big advantage for custom post type that it does not make a mess with regular posts. However, if you want to display them with your regular posts, you can do it easily by adding a simple action hook to the functions.php file.

add_action( 'pre_get_posts', 'add_bangla_post_types_to_query' );

function add_bangla_post_types_to_query( $query ) {
	if ( is_home() && $query->is_main_query() )
		$query->set( 'post_type', array( 'post', 'bangla' ) );
	return $query;
}

Don’t do this if you don’t like to display Bangla and English both on your blog feed. You can use this method for other post types if required.

Step-3: Displaying post with a query:  If you are familiar with WordPress loops you may know how to query a post or post types in a specific template. Use the following code before the closing of your loops to display Bangla post types just beneath your default posts.

<?php 
$args = array( 'post_type' => 'bangla', 'posts_per_page' => 10 );
$bangla = new WP_Query( $args ); 
?>
<?php if ( $bangla->have_posts() ) : ?>
<?php while ( $bangla->have_posts() ) : $bangla->the_post(); ?>
<h2><?php the_title(); ?></h2>
<div class="entry-content">
<?php the_content(); ?> 
</div>
<?php wp_reset_postdata(); ?>
<?php else:  ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>

You can also use the other methods or filters to display the custom post type but if you are not an expert don’t deal with these codes. Simply use them to your specific templates to get the desired result.

Leave a Reply