Guide on custom post types in WordPress from A to Z.

Hướng dẫn custom post type trong wordpress từ a đến z

Today’s lesson focused on custom post types in WordPress, a feature that allows users to create additional areas to post different types of content such as products. By using code in the functions.php file, users can define the parameters for their custom post type, including labels, supported features, and visibility in the admin menu. Additionally, plugins are available to simplify the process of creating custom post types. Overall, custom post types enhance the versatility of WordPress as a CMS, allowing users to tailor their websites to specific needs. Future articles will explore more customization options within WordPress.

Today, let’s delve into a key feature that sets WordPress apart as the most robust CMS globally – custom post types in WordPress. So, what exactly are custom post types in WordPress? Why are they crucial, and how can we effectively utilize them? Let’s uncover all the answers in today’s discussion.

What is a custom post type in WordPress?

When you install WordPress, you’ll notice the default "post" section that manages all the website’s articles. While this suffices for blogs or news sites, it falls short for e-commerce platforms. To post products, we need a separate section. This new product posting area, mimicking the post section’s functionality, is termed a custom post type. WordPress equips us with functions and tools to create one or more custom post types, expanding its usability beyond a mere blogging CMS to accommodate varied site types like sales platforms, real estate portals, corporate websites, services hubs, and more.

See also  Automated WordPress Installation in just 15 seconds

How to create a custom post type in WordPress

To craft custom post types in WordPress, incorporate the following code into the theme’s functions.php file.

Syntax:

function create_custom_post_type(){
    $label = array(
        'name' => 'Products',
        'singular_name' => 'Product'
    );

    $args = array(
        'labels' => $label,
        'description' => 'Post type for product listings',
        'supports' => array(
            'title',
            'editor',
            'excerpt',
            'author',
            'thumbnail',
            'comments',
            'trackbacks',
            'revisions',
            'custom-fields'
        ),
        'taxonomies' => array( 'category', 'post_tag' ),
        'hierarchical' => false,
        'public' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'menu_position' => 5,
        'menu_icon' => 'dashicons-cart',
        'can_export' => true,
        'has_archive' => true,
        'exclude_from_search' => false,
        'publicly_queryable' => true,
        'capability_type' => 'post'
    );

    register_post_type('products', $args);
}
add_action('init', 'create_custom_post_type');

Some notes:

  • The register_post_type function uses a slug and $args parameter.
  • The slug in register_post_type fetches the post type data for frontend display.
  • In $args, ‘supports’ encompasses the features backing the post type. Modify components to streamline the post type.
  • Use menu_icon to select an icon from WordPress’s admin menu icons list.

Upon a successful creation of the custom post type, the products menu will appear in the admin interface.

Creating custom post types in WordPress using a plugin:

Besides code-based creation, numerous plugins in WordPress simplify custom post type setup.

Displaying posts of a custom post type in WordPress

To fetch custom post type posts, utilize the WordPress loop as shown below.

Syntax using new WP_Query:

<?php 
$args = array(
    'post_status' => 'publish',
    'posts_per_page' => -1,
    'post_type' => 'products'
);
$the_query = new WP_Query( $args );

if( $the_query->have_posts() ):
    while( $the_query->have_posts() ) : $the_query->the_post();
        // Retrieve post information
    endwhile;
endif;
wp_reset_query();
?>

Compare this to acquiring regular posts; altering ‘post_type’ to ‘product’ signifies retrieving posts of the product-specific post type.

See also  Adding the "copy code" button to code blocks in Wordpress

File naming for post list and details pages of the custom post type

  • List of all posts: domain.com/product (where ‘product’ is the post type slug) uses archive-product.php.
  • Detail page: single-product.php governs single custom post type pages.

In conclusion,

Learning how to create custom post types signifies harnessing WordPress’s versatility, enabling tailored site functionality. As you explore more WordPress customization aspects, may your theme programming endeavors flourish! Best of luck and hello to all WordPress enthusiasts!

5/5 - (1 vote)

Related posts