WordPress is not only a blogging platform but also a powerful tool for building any type of website. One of the most powerful features of WordPress is the ability to create Custom Post Types (CPT), which allows you to extend the functionality and manage the content of your website with great flexibility. In this article, we will show you how to create and manage Custom Post Types in WordPress.

Table of Contents

    What Are Custom Post Types?

    Custom Post Types are custom content types that you can create in addition to the default types like posts, pages, and media (images, videos). With CPT, you can create special content types like “Products”, “Projects”, “Clients”, “Events” or any other content type that suits your site’s needs.

    Using CPT helps you organize and manage content in a structured way, making it easier to find and display information on your website.

    How to Create Custom Post Types Using Plugin

    One of the easiest ways to create Custom Post Types in WordPress is to use a plugin. There are many plugins that support CPT creation, but the most popular are Custom Post Type UI. Here are the steps to create CPT using this plugin:

    • Go to your WordPress dashboard.
    • Go to item “Plugins” -> “Add New” (Add New).
    • Search for plugins “Custom Post Type UI” and click “Setting” (Install), then click “Activate” (Activate).
    • After activation, go to the “CPT UI” in the control panel and select “Add/Edit Post Types”.
    • Fill out information for your new CPT, including Post Type Slug, Plural Labeland Single Label.
    • Configure options such as feature support (Supports), position in the control panel (Menu Position), and menu icon (Menu Icon).
    • Click “Add Post Type” to complete the CPT creation.
    How to Create Custom Post Types on WordPress
    Create Custom Post Types using Custom Post Type UI plugin on WordPress.

    How to Create Custom Post Types Using Code

    If you want deeper customization or don’t want to use a plugin, you can create a CPT by adding code to the file. functions.php theme or into a custom plugin. Here’s a basic example:

    
    function create_custom_post_type() {
        $labels = array(
            'name' => 'Sản phẩm',
            'singular_name' => 'Sản phẩm',
            'menu_name' => 'Sản phẩm',
            'name_admin_bar' => 'Sản phẩm',
            'add_new' => 'Thêm Sản phẩm',
            'add_new_item' => 'Thêm Sản phẩm mới',
            'new_item' => 'Sản phẩm mới',
            'edit_item' => 'Chỉnh sửa Sản phẩm',
            'view_item' => 'Xem Sản phẩm',
            'all_items' => 'Tất cả Sản phẩm',
            'search_items' => 'Tìm kiếm Sản phẩm',
            'not_found' => 'Không tìm thấy',
            'not_found_in_trash' => 'Không tìm thấy trong thùng rác'
        );
    
        $args = array(
            'labels' => $labels,
            'public' => true,
            'has_archive' => true,
            'rewrite' => array('slug' => 'san-pham'),
            'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'comments'),
            'menu_icon' => 'dashicons-products',
        );
    
        register_post_type('san-pham', $args);
    }
    add_action('init', 'create_custom_post_type');
    
                

    This code will create a new CPT called “Product”, with supporting features like title, content, featured image, and comments. You can customize these parameters to suit your needs.

    Manage and Display Custom Post Types

    Once you create Custom Post Types, you can manage them just like regular posts or pages. In your WordPress dashboard, you’ll see a new section for the CPT you just created, allowing you to add, edit, or delete items within that CPT.

    To display CPTs on your website, you can use widgets, shortcodes, or edit template files in your theme. For example, to display a list of all “Products”, you can use the following code in your template file:

    
     'san-pham',
        'posts_per_page' => 10
    );
    
    $loop = new WP_Query($args);
    
    if ($loop->have_posts()) :
        while ($loop->have_posts()) : $loop->the_post();
            the_title('

    ', '

    '); the_excerpt(); endwhile; else : echo 'Không có sản phẩm nào.'; endif; wp_reset_postdata(); ?>

    This code will display the title and excerpt of the 10 most recent products on your site. You can customize this code to change how it is displayed or how many products are displayed.

    Tips for Using Custom Post Types Effectively

    To get the most out of Custom Post Types, keep in mind the following tips:

    • Clear content classification: Use CPTs to categorize content clearly and easily. This makes it easy for users to find and explore content on your site.
    • Customize display interface: Edit template files to create unique display interfaces for each CPT, making your website look more professional and attractive.
    • Using Custom Taxonomies: Combine CPT with Custom Taxonomies to organize content into custom categories and tags, making it easy for users to find relevant content.

    Create Custom Post Types

    Custom Post Types are a powerful tool that allows you to extend the functionality of WordPress and manage your content with more flexibility. By creating and customizing CPTs, you can turn WordPress into a platform that perfectly fits your needs, whether it’s an e-commerce site, a blog, or a business website. Start exploring and using CPTs today to optimize your website!

    Leave a Reply

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

    Dark mode