How to create custom post type in Wordpress

 First go to functions.php file and new action with new method like below

function university_post_types() {
  register_post_type('event', array(
    'public' => true,
    'labels' => array(
      'name' => 'Events',
    ),
    'menu_icon' => 'dashicons-calendar',
  ));
}

add_action('init', 'university_post_types');

and save and check add some dummy event .

Now question is if someone change the theme what will happens because custom post type in theme folder function.php

function.php not best place to add custom post type code

For custom post type best practice is use Must used Plugin.

For this we will create new folder inside wp-content name -> mu-plugins this always loaded and activated.

Now create file university-post-types.php inside it and paste above custom post types from functions.php and save.


updated code of custom post type event 

function university_post_types() {
  register_post_type('event', array(
    'public' => true,
    'labels' => array(
      'name' => 'Events',
      'add_new_item' => 'Add New Event',
      'edit_item' => 'Edit Event',
      'all_items' => 'All Events',
      'singular_name' => 'Event',
      'search_items' => 'Search Events',
      'not_found' => 'No events found',
     
    ),
    'menu_icon' => 'dashicons-calendar',
  ));
}

add_action('init', 'university_post_types');

I want update above code because when i open http://university.local/event/ archive page not works for this add prperty in array 
 'has_archive' => true,

and i also want to display instead of -> http://university.local/event/ to http://university.local/events/ plural so add below code 

'rewrite' => array('slug' => 'events'),

and salve in setting to permalinks save again then its works

Comments

Popular posts from this blog

Basics Of PHP

Creating App.js for Node Application

WordPress Full Guide For Developer