How to Display Custom Post Types on Your WordPress Site?

0
189

Do you want to learn the way to display custom post types on your WordPress site?

After creating custom post types, the first thing the user has to do is display them on your WordPress site. There is built-in support for displaying your custom post type in WordPress. Make sure you have published a minimum of one article on your custom post type.

In this article, we will cover different methods to display custom post types on your site each one is unique and has its own significance. Let’s begin:

Ways to Display Custom Post Types 

Here are the different methods :

Using Default Archive Template

Firstly, navigate to Appearance >> Menus where you need to add a custom link to your menu. 

Display custom post types on your WordPress site.

The custom link is the link of your custom post type. It varies depending upon the permalinks of your post’s URL. Say you are using SEO friendly permalinks then your URL be like: 

http://example.com/books

However, if you are not using SEO friendly permalink then your URL will most likely be like :

http://example.com/?post­­_type=books

However, if you are not using SEO friendly permalink then your URL will most likely be like :

Display custom post types on your WordPress site.

Make sure you need to replace example.com with your own domain name and the same goes with books with your own custom post type.

Hence, save the menu and head to the front page of your website. You will find that your new menu is added which will display your custom post type.

Display custom post types on your WordPress site.

Using Custom Templates 

This method is for the one who didn’t like the appearance of the archive page for your custom post type. The method is simple just properly follow the steps mentioned below:

Create a new file on your theme directory name it archive-books.php. Note: Don’t forget to replace books with your custom post type. 

In order to start, you need to copy all the contents of your theme’s archive.php file into the archive.books.php template and start making changes to it. Now the template will display the post type after accessing the post type. 

 Also, you can create your own create a file to display a single entry. For this, all you need to do is create single-books.php in the theme directory. Make sure you replace books with your own post type.

Displaying Custom Post type in Widgets

By default, WordPress consists of widgets to display recent posts. Unfortunately, it does not let you choose a custom post type. you need to set a certain system to display custom post types in widgets. For this follow the following steps:

Display custom post types on your WordPress site.
  • After activation, navigate to Appearance >> Widgets. You need to drag and drop the Ultimate post widget to the sidebar.
Display custom post types on your WordPress site.
  • Next, choose the post type and this plugin will display it irrelevant to the post type. Additionally, you can display post excerpts or also show a featured image besides the post title.
Display custom post types on your WordPress site.
  • Remember to save the changes and the plugin will do the rest of the task.

Querying Custom Post Types

Apply this method if only you are perfect at codding. Querying custom post types generally means displaying them along with other post types on the front page. WordPress uses template file index.php to display all the posts on the landing page by default. Certain modification on this template allows you to load custom post type too. Here is an example of a simple index.php file and what sort of changes you can apply to meet the goal:

<?php

$args = array( ‘post_type’ => ‘movies’, ‘posts_per_page’ => 10 );

$the_query = new WP_Query( $args );

?>

<?php if ( $the_query->have_posts() ) : ?>

<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

<h2><?php the_title(); ?></h2>

<div class=”entry-content”>

<?php the_content(); ?>

</div>

<?php endwhile;

wp_reset_postdata(); ?>

<?php else:  ?>

<p><?php _e( ‘Sorry, no posts matched your criteria.’ ); ?></p>

<?php endif; ?>

Hence, you have completed querying custom post types.

Wrapping up

We hope this tutorial was helpful for you to display custom post types on your WordPress site. Follow our other tutorial “How to Add Custom Post Types to Your Main WordPress RSS Feed?” for more advanced information about custom post types. 

LEAVE A REPLY

Please enter your comment!
Please enter your name here