A Comprehensive Guide to Custom Post Types: Unlocking WordPress Flexibility

Create your own Custom Post Types in WordPress

Sometimes we need to create a post-type module, similar to the “posts” in the WordPress website. The creation of such post types is known as Custom Post Types.

Understanding Custom Post Types

In WordPress, a post type is a content type defined by the system to distinguish different types of content on a website. While standard post types include posts and pages, custom post types enable users to create and manage diverse content structures tailored to their specific needs. Examples of custom post types might include portfolios, testimonials, events, products, or any content that deviates from the default post and page formats.

Advantages of Custom Post Types

1. Organized Content

Custom post types allow for the structured organization of diverse content. By categorizing content into specific types, users can efficiently manage and present information on their websites.

2. Improved User Experience

Tailoring the editing interface for each custom post type enhances the user experience. This customization ensures that content creators interact with a streamlined and contextually relevant set of options.

3. Enhanced SEO

Custom post types can be optimized individually, providing an opportunity to boost SEO for specific types of content. This targeted approach helps improve search engine visibility for diverse content types.

4. Flexible Templates

With custom post types, users gain the flexibility to create unique templates for different content types. This enables the design of tailored layouts that align with the specific characteristics of each content category.

Creating Custom Posts Types 

Creating custom post types in WordPress involves defining the characteristics, settings, and features of the new content type. This process typically requires a bit of coding, but there are also plugins available that simplify the creation and management of custom post types for users who prefer a more user-friendly approach.

We prefer you to create a Child Theme of your theme first so that you may reserve your code even after your theme gets updated. Open the functions.php file of your Child Theme and paste the following code before the closing of PHP tags:

// Creates Investors Custom Post Type
function investors_init() {
    $args = array(
      'label' => 'Investors',
        'public' => true,
        'show_ui' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'rewrite' => array('slug' => 'investors'),
        'query_var' => true,
        'menu_icon' => 'dashicons-chart-pie',
        'supports' => array(
            'title',
            'editor',
            'excerpt',
            'trackbacks',
            'custom-fields',
            'comments',
            'revisions',
            'thumbnail',
            'author',
            'page-attributes',)
        );
    register_post_type( 'investors', $args );
}
add_action( 'init', 'investors_init' );
// End Investors Custom Post Type

In this example, a custom post type named ‘Investors’ is created. Adjust the labels, settings, and features according to your specific requirements.

You will see an Investor link will be added.

investors custom post types in wordpress

Now, when you click Add New, you will be redirected to the Add New Investors post. You can enter your content and save the investor posts.

add post form custom post types

You can add the the link for investors page and add them in the menu bar under Appearance > Menu page.

wordpress appearance menu tab

Create Template for Custom Posts Types

In your Child Theme’s directory create a php file “template-investors.php” or any meaningful name you like. At the beginning of the file copy and paste the following lines:

/**
 * Template Name: Investors Single Item
 **/

Now, in the Templates of Add page, you will see the “Investors Single Item” template. In this file copy and paste the following code:

<?php
 $query = new WP_Query( array('post_type' => 'investors', 'posts_per_page' => 5 ) );
 while ( $query->have_posts() ) : $query->the_post(); ?>
// Your code e.g. "the_content();"
<?php endif; wp_reset_postdata(); ?>
<?php endwhile; ?>

Now the theme page you have created will be able to fetch and show the 5 Investors posts.

Tip: If you wish to keep consistency in the theme then copy the template code from the page.php file of your theme paste it into the newly created file and add Query in it to fetch the required posts.

How to show a Featured Image?

If your posts have featured images, then you will have to integrate the featured image code in the template file you have created. Use the following code to make the template able to fetch and show featured images.

<div class="entry-content">
 <?php
    if ( has_post_thumbnail() ) {
      the_post_thumbnail();
    }
      the_content();
  ?>
</div>

As you see the steps are easy and you can easily create as many Custom Posts Types as you need, there is no limit to it.

Just rename the word “Investors” with your desired post type and use it.

Conclusion

Custom post types in WordPress open up a realm of possibilities for website owners seeking to go beyond the standard content structures. Whether you’re running a portfolio website, an e-commerce platform, or a multimedia-rich blog, custom post types empower you to tailor your site’s content organization to your unique needs. Dive into the world of WordPress customization today, and unlock the full potential of your website with custom post types.

Related Posts