Share:
       
   

Create Custom Post Type in WordPress theme no required plugins

In this article today we learn how to create custom post type in your running WordPress theme so If you want to do custom post type in WordPress, then today I will tell you how to use custom post type.

Custom Post Type with custom category in WordPress Without Plugins

you can create your custom post type simply in two steps without any plugins:

Fallow this things.

1. Steps:-

Go to your file manager and then click “wp-content” folder after opened this folder you need to click on “Themes” folder and now click your “active theme” and edit your functions.php file

WordPress Custom Post Type

Right click on functions.php file and select options of edit option button

You just copy and paste your code in your functions.php file in last bottom section.

function create_custom_post_type_reviews() { // change name review name according your post type

$supports = array(
'title', // post title
'editor', // post content
'author', // post author
'thumbnail', // featured images
'excerpt', // post excerpt
'custom-fields', // custom fields
'comments', // post comments
'revisions', // post revisions
'post-formats', // post formats
);

$labels = array(
'name' => _x('Student Reviews', 'plural'),  // change name according your post type
'singular_name' => _x('reviews', 'singular'),
'menu_name' => _x('Student Reviews', 'admin menu'), // change name according your post type
'name_admin_bar' => _x('Student Reviews', 'admin bar'), // change name according your post type
'add_new' => _x('Add New', 'add new'),
'add_new_item' => __('Add New review'),
'new_item' => __('New review'),
'edit_item' => __('Edit review'),
'view_item' => __('View review'),
'all_items' => __('All reviews'),
'search_items' => __('Search reviews'),
'not_found' => __('No reviews found.'),
);

$args = array(
'supports' => $supports,
'labels' => $labels,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'can_export' => true,
'capability_type' => 'post',
'show_in_rest' => true,
'query_var' => true,
'rewrite' => array('slug' => 'reviews'),
'has_archive' => true,
'taxonomies' => array( 'category', 'post_tag' ), // do you need categories and tags?
'hierarchical' => false,
'menu_position' => 10,
'menu_icon' => 'dashicons-format-quote',
);
register_post_type('reviews', $args);
}
add_action('init', 'create_custom_post_type_reviews');

Now you have successfully added custom post type filed in your dashboard see screenshot below of dashboard

without plugins custom post type in WordPress

You can add categories and tags and manage your all custom post like edit, add, delete, search

If you want to use your custom categories name of your custom post then so please update your code in functions.php

you put code just below of this code

add_action('init', 'create_custom_post_type_reviews');
add_action('init', 'create_custom_post_type_reviews');
register_taxonomy('reviews_category', 'reviews', array('hierarchical' => true, 'label' => 'Reviews Categories', 'query_var' => true, 'rewrite' => true));

final code – functions.php is below just copy and paste it.

function create_custom_post_type_reviews() { // change name review name according your post type

$supports = array(
'title', // post title
'editor', // post content
'author', // post author
'thumbnail', // featured images
'excerpt', // post excerpt
'custom-fields', // custom fields
'comments', // post comments
'revisions', // post revisions
'post-formats', // post formats
);

$labels = array(
'name' => _x('Student Reviews', 'plural'),  // change name according your post type
'singular_name' => _x('reviews', 'singular'),
'menu_name' => _x('Student Reviews', 'admin menu'), // change name according your post type
'name_admin_bar' => _x('Student Reviews', 'admin bar'), // change name according your post type
'add_new' => _x('Add New', 'add new'),
'add_new_item' => __('Add New review'),
'new_item' => __('New review'),
'edit_item' => __('Edit review'),
'view_item' => __('View review'),
'all_items' => __('All reviews'),
'search_items' => __('Search reviews'),
'not_found' => __('No reviews found.'),
);

$args = array(
'supports' => $supports,
'labels' => $labels,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'can_export' => true,
'capability_type' => 'post',
'show_in_rest' => true,
'query_var' => true,
'rewrite' => array('slug' => 'reviews'),
'has_archive' => true,
//'taxonomies' => array( 'category', 'post_tag' ), // no need
'hierarchical' => false,
'menu_position' => 10,
'menu_icon' => 'dashicons-format-quote',
);
register_post_type('reviews', $args);
}
add_action('init', 'create_custom_post_type_reviews');
register_taxonomy('reviews_category', 'reviews', array('hierarchical' => true, 'label' => 'Reviews Categories', 'query_var' => true, 'rewrite' => true));

Custom Post Type with categories

Create Custom post type Without WordPress Plugins

2. Steps:-

We learn in 1step that how to add code in functions.php file for custom post type without plugins.

Now in 2nd step you need to display you post on page. how to show my all custom post type fields in front – end.

Simply go to your page template where you want to show your custom post type and paste below code in your page. like create your template-review.php. you can put file on root or in template folder in your theme.

<?php

/*Template Name: Review Template*/
?>

<?php get_header();
query_posts(array(
   'post_type' => 'reviews'
)); ?>
<div class="container">
  <ul>
<?php
while (have_posts()) : the_post(); ?>
<li><h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
<p><?php //the_excerpt(); ?></p>
</li>
<?php endwhile;?>
</ul>
</div>   
<?
get_footer();

Custom post Type post with custom Category

About Raka Rocks

Hi, My self Raka Rocks. I am web designer and Front End Developer total 8 year exp. in this fields. I am a continuous blogger and has blogged on different topics. He loves to surf the Internet and always tries to get new ideas about new technologies and innovations and share this excellent information with all technology lovers.

Leave a Reply

Your email address will not be published.