• About
  • Write For Us
  • Contact
  • Top 50 Photography Blogs
  • Resources
  • Forum

IndustryDev

  • Design
    • Photoshop
    • Lightroom
    • Camera Raw
    • Bridge
  • Development
    • HTML
    • CSS
    • Javascript
    • PHP
    • Dev Tools
    • WordPress
  • Photography
  • Blogging
  • Technology
  • Inspiration
You are here: Home / Wordpress / Creating WordPress Custom Post Type Templates

Creating WordPress Custom Post Type Templates

June 2, 2015

Creating WordPress Custom Post Type Templates

There seems to be a bit of debate out there about whether or not to use a group of plugins to assist in creating custom post type template for your WordPress site. Zac Gordon, from Treehouse, advocates the use of the plugins. He says that by utilizing these plugins, a website owner is protected from custom post type loss if they decide to change themes. His favorite plugins to accomplish the creation of these templates are:

Advanced Custom Fields – (Owner’s Website)

Custom Post Type UI – (Owner’s Website)

Over at WPBeginner, it’s said that there’s danger in using plugins to create your custom post types. If a user deactivates one of the plugins involved, they run the risk of losing the posts, since those types aren’t registered in WordPress. They also won’t be available in the admin section.

When I began writing this post, I leaned towards the view of hard coding the custom post type code into the necessary templates. I’m still leaning that way. I’m not one to believe that users should flip through themes in their WordPress back-end and expect various features to continue working as they did before. To say that someone is only going to lose their custom post types by changing themes is only partially true. They’ll lose much more than that. Basically, anything that was coded into their template files will be gone. So why limit your concern to custom post types?

By the way, I’m really not going to go into crazy details on this topic in this post. I’ll cover the essentials, but if you’re looking for more, please feel free to take a look at these posts:

How to Create Custom Post Types in WordPress by WPBeginner

Creating Custom Post Types In WordPress by Elegant Themes

In this post, I’m going to focus on setting up custom post types by using code. If you read my previous post on setting up navigation, this shouldn’t look too foreign to you.


To keep things as simple as possible, I’m going to tell you that in order to get everything set up, we’ll need to complete three steps.

1. Give WordPress all the information it’ll need to recognize our custom post types (register our custom post type).
2. Create a special archive template that will hold all our custom posts (like an archive or category page).
3. Create a custom post template.

See, that’s not so terrible.

Step 1

I’m going to review some code from the WordPress codex and use parts of it in the code examples below. And just to make you aware, I’m getting this from the “register_post_type” page, among other sources.

// Custom Post Type Function
function book_reviews_posttype() {
    // Custom Post Type Options
    $args = array(
      'label' => 'Book Reviews',
        'public' => true,
        'show_ui' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'rewrite' => array('slug' => 'book-reviews'),
        'query_var' => true,
        'supports' => array(
            'title',
            'editor',
            'excerpt',
            'trackbacks',
            'custom-fields',
            'comments',
            'revisions',
            'thumbnail',
            'author',
            'page-attributes',)
        );
    register_post_type( 'book-reviews', $args );
}
// Hooking Our Function Into the Theme (calling the function)
add_action( 'init', 'book_reviews_posttype' );


The above block of code goes inside your functions.php file. Now, if you’re familiar with PHP, you might be aware that you can write this code another way. Instead of creating and using a local $args variable to store our options, we can include the array right inside the register_post_type function.

// Custom Post Type Function
function book_reviews_posttype() {
  register_post_type( 'book-reviews', 
  // Custom Post Type Options
    array(
      'label' => 'Book Reviews',
        'public' => true,
        'show_ui' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'rewrite' => array('slug' => 'book-reviews'),
        'query_var' => true,
        'supports' => array(
            'title',
            'editor',
            'excerpt',
            'trackbacks',
            'custom-fields',
            'comments',
            'revisions',
            'thumbnail',
            'author',
            'page-attributes',)
    ) 
  );
}
// Hooking Our Function Into the Theme (calling the function)
add_action( 'init', 'book_reviews_posttype' );

It’s up to you which way you want to go. Also, to get a handle on what all these arguments mean, check out the WordPress Codex page.

If everything went smoothly during the setup of your functions.php code, you should now have an new menu link in the admin. If you don’t, check over your code. If it’s all okay, click around a bit to see what posting something new would feel like.

——

Just a quick note: now that we have the ability to create new custom posts in our admin, we still have no way of seeing those posts on our website. The area that holds our primary custom page and posts isn’t automatically linked to. In order to link to it, we’ll need to modify an existing, or create a new, menu with our static link. In this case, the link to our custom area would be http://www.example.com/book-reviews/.

——

Step 2

If we leave things the way they are, WordPress will use our fallback archive.php template to display the list of our book review custom posts, as can be seen in the hierarchy. If we don’t like this template or want to modify it, we can do so by creating a file called, “archive-book-reviews.php” and populating it with our template code. We’ll need to include the correct text commented out at the top of the page.

/**
 * Template Name: Book Reviews
 **/

The comment above tells WordPress that it’s not only a custom template, but the template we’re using for our book reviews.

Inside the template, we’re going to need to include a loop that will correctly call the data we’d like to be called. So, within your formatted HTML code, you’d use something like this:

<?php
 $query = new WP_Query( array('post_type' => 'book-reviews', 'posts_per_page' => -1 ) );
 while ( $query->have_posts() ) : $query->the_post(); ?>
<?php the_content(); ?>
<?php endif; wp_reset_postdata(); ?>
<?php endwhile; ?>

Step 3

In this last step, we’re going to create a new custom post template for each of our custom posts. As it stands now, WordPress will use the “single.php” template for this. Since we’re creating something unique, we most likely want to change that. To follow the WordPress naming convention, we’ll call our new file, “single-book-reviews.php.” Take a look at the code below:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>      
  <h1><?php the_title(); ?></h1>
    <p><?php the_field('description'); ?></p>
    <p><?php previous_post_link(); ?> -  <a href="<?php bloginfo('url'); ?>/book-reviews">Back to Reviews</a> -   <?php next_post_link(); ?></p>   
<?php endwhile; endif; ?>

In the most basic sense, the stripped down code above should get each post showing on its own page. Of course, you’ll want to add your own formatting, but as far as creating the PHP code goes, this is good.


——

I’m actually going to revisit this post in the future to clean up a few parts. I believe the steps are the correct approach to take when attempting to explain this, sort of confusing, task, but as far as the actual PHP goes, I would like to verify its accuracy. What I’m learning is that there are multiple ways to accomplish the same task and many resources online conflict with one another. The only way I’ll be able to rectify this is to gain a better understanding of the functions in question.

Treehouse just released a new course on PHP for WordPress that I’d like to take. It’s obvious that any WordPress developer needs to have a solid understanding of PHP to truly delve into development of this sort. I’ll be taking this course shortly.

If you’re interested in my other posts on this topic, please take a look at the WordPress category. And as always, if you have comments or questions, please leave them below.

Related posts:

  1. WordPress Custom Post Type Templates
  2. Setting Up Navigation In WordPress
  3. WordPress Page & Post Templates
  4. Creating WordPress Page Templates
  5. Creating Blog Templates in WordPress

Filed Under: Wordpress

What’s Next? Email Updates!

If you enjoyed reading this post, why not consider signing up to receive others like it by email? It's so easy and you can unsubscribe at any time.

Leave a Reply Cancel reply

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

Connect With Me

  • Facebook
  • Instagram
  • Pinterest
  • RSS
  • Twitter

MOST POPULAR POSTS

  • How to Set the Photo Quality in your Canon Rebel DSLR Camera Before participating in any type of photo shoot, it's i...
  • How to Adjust the Mouse Click & Scroll Settings in Windows 10 I's say this is one of the very first settings I ed...
  • How to Export Video From Adobe Photoshop When it comes to exporting and rendering video clips, t...
  • How to Apply an Adjustment to Only One Layer in Adobe Photoshop The answer is clipping. I'll tell you that right up fro...
  • How to Speed Up & Slow Down Video in Adobe Photoshop This is one of those posts that's going to be super hel...
  • Cutting Out a Shape From a Shape in Adobe Photoshop I've been using shapes for various things in Adobe Phot...
  • How to Set the Self Timer On Your Canon Rebel DSLR Camera Camera self timers are great. I was recently part of a...
  • Animating Scale, Rotation & Opacity in Adobe Photoshop I sat down a few days ago and started messing around in...
  • How to Set Your Canon Rebel Camera For Continuous Shooting Continuous Shooting mode is very important for those wh...
  • 3 Ways to Close Applications in Windows 10 This is going to be a very quick post because the topic...

Recent Comments

  • angelica blanco on How to Organize Video & Audio Project Files for Adobe Photoshop
  • pete salomone on Downloading Photos From a Digital Camera Using Adobe Bridge
  • cdn on How To Create a Slideshow For Your Lock Screen in Windows 10
  • Jay Gaulard on How to Set Your Canon Rebel Camera For Continuous Shooting
  • Deb on How to Set Your Canon Rebel Camera For Continuous Shooting

Copyright © 2021 · IndustryDev.com Web Design & Photography Blog · About · Write For Us · Contact · Privacy / Terms · Sitemap