wordpress-custom-post-type-integration

Creating Custom Post Types In WordPress

Maximise your content potential with custom post types.

Facebook
Twitter
Reddit

WordPress is a widely used content management system, known for its intuitive user interface and powerful functionality. Out of the box, WordPress provides three core content types: Posts, Pages, and Media, which were more than sufficient for setting up and running a website in the early days of WordPress.

However, as the platform has evolved, the demands placed upon it have become increasingly complex. In response, WordPress has grown in both flexibility and sophistication, offering new ways to manage content through the use of custom post types.

Custom post types allow you to create additional content types that are tailored to your specific needs. By doing so, you can enhance the user experience on your site and better organize your content.

If you’re interested in taking your WordPress site to the next level, it’s worth exploring the possibilities of custom post types. In this article, we’ll take you through a step-by-step guide to creating a custom post type in WordPress, so you can start leveraging this powerful feature to your advantage.

What is a Custom Post Type?

Custom Post Types, also known as “Custom Content Types,” are additional post types that can be added to your WordPress website using a simple function called “register_post_type().” This function enables you to add new post types with specific features, availability, and labels, allowing you to tailor your website to your unique needs.

WordPress offers support for an unlimited number of Custom Post Types, providing you with endless possibilities for creating custom posts and displaying them wherever you choose. For instance, if you own a recipe website and want to add a custom post type called “Recipes,” you can easily create it using the register_post_type() function. Once created, the Recipes post type will have its own menu in the WordPress dashboard admin area, allowing you to manage it independently from other post types.

Creating a Custom Post Type

Custom Post Types can be added to any WordPress theme. Simply open the functions.php from your WordPress theme directory and add the following code.

/* Custom Post Type Start */
 
function create_posttype() {
register_post_type( 'recipes',
// CPT Options
 
array(
  'labels' => array(
   'name' => __( 'Recipes' ),
   'singular_name' => __( 'Recipes' )
  ),
  'public' => true,
  'has_archive' => false,
  'rewrite' => array('slug' => 'recipes'),
 )
);
}
// Hooking up our function to theme setup
add_action( 'init', 'create_posttype' );
 
/* Custom Post Type End */

After adding this code, save your functions.php file and refresh WordPress; you should now see a new tab appear in the WordPress admin area.

create-wordpress-custom-post-type

When you create a custom post types, it is necessary to use init for the hook in add_action(). The register_post_type() function takes the arguments.

/*Custom Post type start*/

function cw_post_type_recipes() {

$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('Recipes', 'plural'),
'singular_name' => _x('Recipes', 'singular'),
'menu_name' => _x('Recipes', 'admin menu'),
'name_admin_bar' => _x('Recipes', 'admin bar'),
'add_new' => _x('Add Recipe', 'add new'),
'add_new_item' => __('Add New Recipe'),
'new_item' => __('New Recipe'),
'edit_item' => __('Edit Recipe'),
'view_item' => __('View Recipes'),
'all_items' => __('All Recipes'),
'search_items' => __('Search Recipes'),
'not_found' => __('No Recipes found.'),
);

$args = array(
'supports' => $supports,
'labels' => $labels,
'public' => true,
'query_var' => true,
'rewrite' => array('slug' => 'recipes'),
'has_archive' => true,
'hierarchical' => false,
);
register_post_type('recipes', $args);
}
add_action('init', 'cw_post_type_recipes');

/*Custom Post type end*/

Displaying the Detail Page of your new Custom Post Type

Just like a default WordPress post, your new custom post type will need its own detail page template file. To do so, add a new file called single-recipe.php which is located in your WordPress theme and then add the following code to it.

<?php
get_header();
/* Start the Loop */
while (have_posts()) : the_post();
   get_template_part('template-parts/post/content', get_post_format());
endwhile; // End of the loop.
get_footer();

Don’t settle for a generic WordPress site. Take your online presence to the next level with custom WordPress development services. As a professional freelance web developer, I specialise in creating tailored solutions that align with your business goals and engage your audience. Let’s collaborate to bring your WordPress website to life. Contact me today to get started!

Facebook
Twitter
Reddit

Get In Touch