How to create a custom post type in Wordpress?

by princess.fritsch , in category: PHP , 2 years ago

How to create a custom post type in Wordpress?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

by yadira.tillman , 2 years ago

@princess.fritsch You can create our own custom post types in Wordpress besides the default post. We have to use wordpress core to implement this feature. We can add Product type, property type using Custom Post Type. Example is given as follows:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
<?php
function create_post_type() {
      register_post_type( 'My Custom Post Type',
        array(
          'labels' => array(
            'name' => __( 'Custom Post Type Name' ),
            'singular_name' => __( 'Custom Post Type Singular' )
          ),
          'public' => true,
          'has_archive' => true,
        )
      );
    }
?>
add_action( 'init', 'create_post_type' );


Member

by kendrick , a year ago

@princess.fritsch 

To create a custom post type in WordPress, you'll need to use the register_post_type() function. This function allows you to create a new post type by specifying a few parameters. Here's an example of how you can use it:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
function create_custom_post_type() {
  register_post_type( 'custom_type',
    array(
      'labels' => array(
        'name' => __( 'Custom Types' ),
        'singular_name' => __( 'Custom Type' )
      ),
      'public' => true,
      'has_archive' => true,
      'rewrite' => array('slug' => 'custom-type'),
    )
  );
}
add_action( 'init', 'create_custom_post_type' );


This code creates a new custom post type called "Custom Type," which will be publicly available and will have its own archive page. You can customize the post type further by specifying additional parameters in the register_post_type() function.


Once you've added this code to your WordPress site, you'll be able to create and manage custom post types just like you would regular posts. You can do this by going to the Posts menu in the WordPress dashboard and clicking on the "Add New" option under the Custom Types section.


I hope this helps! Let me know if you have any other questions.