WordPress

Create Custom Post Type in WordPress

0
WordPress

To create a custom post type in WordPress, you can use one of the following methods:

Method 1: Using the WordPress dashboard

  1. Go to Settings > Post Types.
  2. Click on the Add New button.
  3. Enter a name and slug for your custom post type.
  4. Select the desired capabilities for your custom post type.
  5. Click the Add Post Type button.

Method 2: Using a WordPress plugin

There are also a number of WordPress plugins that can be used to create custom post types. One popular plugin is called Custom Post Type UI.

To use this plugin:

  1. Install and activate the plugin.
  2. Go to CPT UI > Add/Edit Post Types.
  3. Click on the Add New button.
  4. Enter a name and slug for your custom post type.
  5. Select the desired options for your custom post type.
  6. Click the Add Post Type button.

Method 3: Using code

You can also create custom post types using code. To do this, you need to add a function to your theme’s functions.php file.

The following code shows an example of how to create a custom post type called portfolio:

PHP
function create_portfolio_post_type() {
    register_post_type( 'portfolio',
        array(
            'labels' => array(
                'name' => 'Portfolio',
                'singular_name' => 'Portfolio Item',
            ),
            'public' => true,
            'has_archive' => true,
            'supports' => array( 'title', 'editor', 'thumbnail' ),
        )
    );
}

add_action( 'init', 'create_portfolio_post_type' );

Once you have added the code to your theme’s functions.php file, you need to activate your theme.

Custom post type taxonomies

You can also create custom taxonomies for your custom post types. Taxonomies are used to categorize and organize your custom post types.

To create a custom taxonomy, you can use the register_taxonomy() function. The following code shows an example of how to create a custom taxonomy called portfolio_categories for the portfolio custom post type:

PHP
function create_portfolio_categories_taxonomy() {
    register_taxonomy( 'portfolio_categories',
        array( 'portfolio' ),
        array(
            'labels' => array(
                'name' => 'Portfolio Categories',
                'singular_name' => 'Portfolio Category',
            ),
            'hierarchical' => true,
        )
    );
}

add_action( 'init', 'create_portfolio_categories_taxonomy' );

Once you have created a custom taxonomy, you can assign it to your custom post type in the WordPress dashboard. To do this, go to Settings > Post Types. Click on the name of your custom post type and then select the Taxonomies tab. Select the custom taxonomy that you want to assign to your custom post type and then click the Add Taxonomy button.

Custom post type templates

You can also create custom templates for your custom post types. This allows you to customize the appearance of your custom post types.

To create a custom template, create a new file in your theme’s directory and name it single-[custom-post-type].php. For example, if you created a custom post type called portfolio, you would create a file called single-portfolio.php.

In the custom template file, you can add the code that you want to appear on the single post page for your custom post type. You can also use the WordPress template system to add additional features to your custom template, such as sidebars and footers.

Conclusion

Creating custom post types is a great way to organize your content and make it easier for your visitors to find the information they are looking for. By following the steps above, you can create custom post types that are tailored to your specific needs.

Remove Query Strings as shown in GTMetrix

Previous article

Get Post Page URL in WordPress

Next article

You may also like

Comments

Leave a reply

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

More in WordPress