How to Create Custom Post Types in WordPress

As you can see, we have added many more options to the custom post type with his code. It will add more features like support for revisions, featured image, custom fields, etc.

If you are working on a client site and do not want to install another plugin, then you can manually create your custom post type by adding the required code in your theme’s functions.php file or in a site-specific plugin (See: Custom Post Types Debate functions.php or Plugin).

function my_custom_posttypes() {
  $labels = array(
        'name' => 'Service',
        'singular_name' => 'Service',
        'menu_name' => 'Service',
        'name_admin_bar' => 'Service',
        'add_new' => 'Add New Service',
        'add_new_item' => 'Add New Service',
        'new_item' => 'New Service',
        'edit_item' => 'Edit Service',
        'view_item' => 'View Service',
        'all_items' => 'All Service',
        'search_items' => 'Search Service',
        'parent_item_colon' => 'Parent Service:',
        'not_found' => 'No Service found.',
        'not_found_in_trash' => 'No Service found in Trash.',
    );
    $args = array(
        'labels' => $labels,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'menu_icon' => 'dashicons-admin-post',
        'query_var' => true,
        'rewrite' => array('slug' => 'service'),
	'publicly_queryable'  => false,
        'capability_type' => 'post',
        'has_archive' => true,
        'hierarchical' => false,
        'menu_position' => 5,
        'supports' => array('title', 'editor', 'revisions', 'custom-fields', 'thumbnail')
    );
    register_post_type('service', $args);
   } 

add_action('init', 'my_custom_posttypes');

Displaying Custom Post Types on The Front Page & Any Page

If you are familiar with the coding and would like to run loop queries in your templates, then here is how to do that (Related: What is a Loop?).

In this code, first we have defined the post type and posts per page in the arguments for our new WP_Query class. After that we ran our query, retrieved the posts and displayed them inside the loop.

<?php

if ( get_query_var('paged') ) $paged = get_query_var('paged');
if ( get_query_var('page') ) $paged = get_query_var('page');

$query = new WP_Query( array( 'post_type' => 'service', 'posts_per_page' => 1, 'paged' => $paged ) );

if ( $query->have_posts() ) : ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
    <div class="col-xl-3 col-sm-6 col-md-4 col-lg-3">
     <?php echo get_the_post_thumbnail( $page->ID, 'medium' ); ?>
        <div class="entry">
         <h3><?php the_title(); ?></h3>
         <p><?php the_excerpt(); ?></p>
           <p>
          <?php
          $char_limit = 95; //character limit
          $content = $post->post_content; //contents saved in a variable
          echo substr(strip_tags($content), 0, $char_limit);
          ?></p>

          <a href="<?php the_permalink() ?>" class="btn-red">Read More</a>
        </div>
    </div>
<?php endwhile; wp_reset_postdata(); ?>
<!-- show pagination here -->
<?php else : ?>
<!-- show 404 error here -->
<?php endif; ?>

We hope this article helped you learn how to create custom post types in WordPress.

If you liked this article, then please more WordPress Post view now.

niteshbhardwaj.com in Delhi NCR, niteshbhardwaj.com in Laxmi nagar, niteshbhardwaj.com in Ganesh nagar, niteshbhardwaj.com in shakarpur, niteshbhardwaj.com in Ramesh park, niteshbhardwaj.com in Nirman Vihar, niteshbhardwaj.com in Geeta colony, niteshbhardwaj.com in Mandawali, niteshbhardwaj.com in west vinod nagar, niteshbhardwaj.com in karkardooma, niteshbhardwaj.com in Preet vihar, niteshhardwaj.com in Shadipur, niteshbhardwaj.com in Rajouri garden, niteshbhardwaj.com in Bara khamba, niteshbhardwaj.com in yamuna bank, niteshbhardwaj.com in joshi colony, niteshbhardwaj.com in Chandar vihar, niteshbhardwaj.com in Manglam School, niteshbhardwaj.com in Madhu vihar, niteshbhardwaj.com in Mayur vihar, niteshbhardwaj.com in Alla colony, niteshbhardwaj.com in Dwarka, niteshbhardwaj.com in Noida, niteshbhardwaj.com in Greater Noida