WordPress

Create custom posts in WordPress

0
1k
2017 / 11 / 09

Sometimes we need to create our own posts type similar module in our WordPress website. This type of post is normally called as Custom Posts Type.

 

What are Custom Posts Type and why we need them? 

This is not a rocket science. Its not more then that of the posts a user add in his/her site as a blog. There can be any purpose of Custom Posts Types, depends on the requirement or feature of project. In this tutorial  I will be going to explain on how we can create Custom Posts Type.

They are used to structure your website or remove complexisites of site by dividing the posts into different sections.

 

I prefer you to create a Child Theme of your theme first so that you may reserve your code even after your theme get updates.

Open functions.php file of your Child Theme and paste the following code before 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

You will see an Investor link will be added.

 

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.

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

 

Create Custom Posts Type Template

In your Child Theme directory create a phop file “template-investors.php” or any meaningful name you like. In the begining of 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 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 theme then copy the template code from page.php file of your theme and paste it in the newely created file and add Query in it to fetch the required posts.

 

How to show Featured Image?

If your posts has featured images, then you will have to integrate featured image code in the template file you have created. Use the following code too 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 much Custom Posts Types as you need, there is no limit in it.

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

 

 

Hi, I am a full-stack web developer with 5+ years of experience in working with different web technologies. Do you want to ask something? just send me a message through the contact form. Please visit my portfolio at hamzamehmood.com. Thanks