Home » Blog » How to display Custom Post Types in WordPress
Blog

Custom Post Types make WordPress a true content management system.

You can structure and organize content beyond pages and posts, which WordPress serves up by default, but lacks in the creation of custom content. Want a collection of books or team members? No problem, CPT’s can do that. You can even include “custom fields” which you can image are just fields of specific data or content, per CPT. An example would be something like having a “Team” CPT with custom fields for each team member’s social media links, and e-mail fields. Stuff you want to live outside of the typical editor.

Fun stuff — but — the act of creating & displaying your CPT’s, isn’t something that WordPress is setup to do out of the box, for the average user. You’re going to need to have a handle on some basic coding skills, if you’re planning on doing this the manual way. This is great practice for those of you that want to take your WordPress development skills to the next level.

tl;dr If you want the easy way to create CPT’s and display them, checkout Custom Post Type Maker to create your CPT’s and of course, Conductor Plugin to display them!

Custom Post Types vs Posts & Pages

As I mentioned above, CPT’s expand upon the basic idea of a post or page, by giving you full control over the content stored in WordPress. I created a video that gives you a full explanation of CPTs that you can watch here:

Creating your Custom Post Type

For those of you looking to do things the manual way, I’ll provide a code snippet that registers your CPT in WordPress. This code must reside in your functions.php file:

// Register Custom Post Type
function custom_post_type() {

	$labels = array(
		'name'                  => _x( 'Post Types', 'Post Type General Name', 'text_domain' ),
		'singular_name'         => _x( 'Post Type', 'Post Type Singular Name', 'text_domain' ),
		'menu_name'             => __( 'Post Types', 'text_domain' ),
		'name_admin_bar'        => __( 'Post Type', 'text_domain' ),
		'archives'              => __( 'Item Archives', 'text_domain' ),
		'attributes'            => __( 'Item Attributes', 'text_domain' ),
		'parent_item_colon'     => __( 'Parent Item:', 'text_domain' ),
		'all_items'             => __( 'All Items', 'text_domain' ),
		'add_new_item'          => __( 'Add New Item', 'text_domain' ),
		'add_new'               => __( 'Add New', 'text_domain' ),
		'new_item'              => __( 'New Item', 'text_domain' ),
		'edit_item'             => __( 'Edit Item', 'text_domain' ),
		'update_item'           => __( 'Update Item', 'text_domain' ),
		'view_item'             => __( 'View Item', 'text_domain' ),
		'view_items'            => __( 'View Items', 'text_domain' ),
		'search_items'          => __( 'Search Item', 'text_domain' ),
		'not_found'             => __( 'Not found', 'text_domain' ),
		'not_found_in_trash'    => __( 'Not found in Trash', 'text_domain' ),
		'featured_image'        => __( 'Featured Image', 'text_domain' ),
		'set_featured_image'    => __( 'Set featured image', 'text_domain' ),
		'remove_featured_image' => __( 'Remove featured image', 'text_domain' ),
		'use_featured_image'    => __( 'Use as featured image', 'text_domain' ),
		'insert_into_item'      => __( 'Insert into item', 'text_domain' ),
		'uploaded_to_this_item' => __( 'Uploaded to this item', 'text_domain' ),
		'items_list'            => __( 'Items list', 'text_domain' ),
		'items_list_navigation' => __( 'Items list navigation', 'text_domain' ),
		'filter_items_list'     => __( 'Filter items list', 'text_domain' ),
	);
	$args = array(
		'label'                 => __( 'Post Type', 'text_domain' ),
		'description'           => __( 'Post Type Description', 'text_domain' ),
		'labels'                => $labels,
		'supports'              => array( ),
		'taxonomies'            => array( 'category', 'post_tag' ),
		'hierarchical'          => false,
		'public'                => true,
		'show_ui'               => true,
		'show_in_menu'          => true,
		'menu_position'         => 5,
		'show_in_admin_bar'     => true,
		'show_in_nav_menus'     => true,
		'can_export'            => true,
		'has_archive'           => true,		
		'exclude_from_search'   => false,
		'publicly_queryable'    => true,
		'capability_type'       => 'page',
	);
	register_post_type( 'post_type', $args );

}
add_action( 'init', 'custom_post_type', 0 );

As long as this snippet of code remains active in your functions.php, your CPT is registered and active on your website. If you remove this snippet of code, while you lose the “active” state of your CPT, any data submitted through the CPT is still saved in your WordPress database.

You can continue to create as many CPT’s as you want, by replicating this code code in your functions.php file.

If you don’t want to mess with code, we’ll use the plugin Custom Post Type Maker, to quickly make our CPTs, so we don’t have to write any code.

<insert screenshot>

This plugin makes CPT creation much easier, without the overhead of worrying about editing code and files of your WordPress website. Let’s move onto the next piece, displaying your CPT data.

Displaying Custom Post Types on your WordPress website

Now it’s time to display those wonderful CPT’s on your site, while comparing and contrasting between the “manual coding” way versus using the Conductor plugin.

This snippet of code can be applied to a specific WordPress template file, which would allow you to display a collection of CPTs.

<?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' => 'books', 'paged' => $paged ) );

if ( $query->have_posts() ) : ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="entry">
<h2 class="title"><?php the_title(); ?></h2>
<?php the_content(); ?>
</div>
<?php endwhile; wp_reset_postdata(); ?>
<!-- show pagination here -->
<?php else : ?>
<!-- show 404 error here -->
<?php endif; ?>

If you’re planning on displaying a collection of CPTs on one single page, this straight-forward approach might be all you need. However, you’re still having to write code, and as soon as you need to create multiple pages or need to style that output, this method quickly becomes cumbersome.

That’s where using a builder plugin like Conductor becomes a huge advantage to creating pages of content, so much faster. This video will show you how to use Conductor to display your CPT’s and custom fields, in about 1 minute:

Conductor is like Drupal’s Views, but for WordPress. We make it super easy to display content residing in your WordPress database, on any page or post, with a widget or a short code.

Watch more videos on Conductor here or get the plugin today!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.