How to create a custom post type for Bangla Blog Section

Today, we will learn how to create a custom post type for Bangla Blog Section and how to query this post type in a specific custom post type template.

Why Custom Post Type for Bangla

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 but none of them fully satisfy me. Because these methods are just extended my workloads. For example, I can’t use the same image from a single upload to my Bengali version and having some 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 a single upload and developing Bengali posts from the same dashboard. It reduces my workload, and I can create a blog from my main website using the same login, the same image, and attach them to another version of the blog, for Bangla, and for English both.

A great solution for multilingual website

If you are thinking to create a different version of your blog for Bangla or for your native language, using a custom post type can be a great solution. To create a custom post type you can use the WordPress Codex Developer Handbook or you use a wp code generator tool. Here I am sharing the piece of code to create a custom post type for Bangla or in your native language. You can change the word Bangla for your desired language. For instance, Hindi, Urdu, Arabic, etc.

How to Create Custom Post Type

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 your 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 as a regular post:

It is a big advantage for custom post types 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 create queries for a post or post types in a specific template. Use the following code before the closing of your loops to display Bangla post types after your default post.

<?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

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.