Blog Posts

Create Custom Post Type in WordPress Without a Plugin

Create Custom Post Type in WordPress Without a Plugin

Create Custom Post Type in WordPress Without a Plugin

WordPress isn’t just for blogging purposes. Using custom post types, you can add and display content beyond posts or pages. This way, you can easily turn your WordPress site into a powerful CMS.

What are custom post types in WordPress?Assign the newly create

How to create a custom post type?

Let’s discuss.

What is a Custom Post Type (CPT)?

WordPress comes with various standard post types-

  • Post
  • Page
  • Attachment
  • Revision
  • Nav_menu_item
  • Custom_css
  • Customize_changeset
  • Oembed_cache
  • User_request
  • Wp_block

A custom post type refers to the post type that allows you to create content in different formats than the standard formats available in WordPress.

How to Create a Custom Post Type in WordPress?

To create a custom post type with the name “Store”, follow the stepwise procedure-

  1. Use the register hook to “register_post_type”
  2. Open functions.php file in the theme
  3. Copy the following code and paste it in the functions.php file.


    			
    			/********************Custom Post Type****************************/
    			function storePostType() {
    				register_post_type( 
    					'stores',
    					// CPT Options
    					array(
    						'labels' => array(
    							'name' => __( 'Stores' ),
    							'singular_name' => __( 'Store' ),
    							'add_new_item' => __( 'Add New Store', 'text_domain' )
    						),
    						'supports' => array('title','editor','author','thumbnail' ),
    						'public' => true,
    						'has_archive' => true,
    						'rewrite' => array('slug' => 'stores'),
    						'show_in_rest' => true,
    						'menu_position' => 5,
    						'menu_icon' => 'dashicons-store',
    					)
    				);
    			}
    			// Hooking up our function to theme setup
    			add_action( 'init', 'storePostType' ); 
    
    			/**********************Custom Post Type****************************/
    		
  4. Refresh the browser
  5. Your new custom post type is ready. Find it in the WordPress admin panel, and use it for posting your custom content.



Also Read- WordPress: Preventive Measures For Theme Change

If you want to change the name of the custom post type, simply replace “Store” with the new name.



Note: In WordPress, you can change the icon and position of the custom post type by modifying the following parameters-

'menu_position' => 5,
'menu_icon' => 'dashicons-store',

To check the available menu icons in WordPress, click on the following link

https://developer.wordpress.org/resource/dashicons

How to Display Content on The Front-end?

To display content on the front-end, create a custom template in the activated theme folder. Here’s the stepwise process-

  1. Create a file template-stores.php in the activated theme. Paste the following code in it.


  2. Assign the newly created custom template to page by following these steps-
    <?php /* Template Name: Stores */ ?>
    <?php get_header();?>
    <?php get_footer();?>
    
    
    • Open the admin panel & add a new page. Select the template from the template dropdown menu on the right hand side and save it.

    • Open the custom template file and add the following code-

      			<?php	/* Template Name: Stores*/
      				get_header(); ?>
      					<table>
      	    				<?php query_posts(array(
      	    					'post_type' => 'stores'
      	    				));
      	   
      	    				while (have_posts()) : the_post(); ?>
      	        				<tr>
      				            <td><h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2></td>
      				            <td><p><?php the_content(); ?></p></td>
      				            <td><?php the_post_thumbnail('thumbnail'); ?> </td>
      	        				</tr>
      	    				<?php endwhile; ?>    
      					</table>
      				<?php get_footer();?> 

      Note: You have to add your own post slug in the “post_type” parameter. Open your custom post type and find the slug in the URL.

How to Create a Custom Post Detail Page?

  1. Open theme and find single.php or singular.php (default post detail page)
  2. Copy and paste this file
  3. Rename the newly added file by adding your custom post slug in it. For example: single-stores.php or singular-stores.php.

Hope this guide helped you learn how to create a custom post type in WordPress without using plugin. If you liked it, share it with others.
For any kind of professional help related to WordPress development, hire expert developers at Tech Prastish Software Solutions Pvt Ltd. For more details, contact us.

We are Hiring We’re hiring Opportunities for fresher Fresher opportunities