How to add Custom Post Types to Thesis 2.x

Custom post types are nothing new in WordPress but since thesis 2.1 added a query box to the mix I figured I would show you how to take advantage of this and use them to diversify your content. Typically this would be subject to coding custom loops but the Thesis 2.x query box takes away that aspect of work by making it really simple.

Each Thesis skin comes with a certain file structure.

thesis 2.1 skin file structure

There are two ways in which you can add custom post types.

The first method is via custom.php which is very much like the functions.php found in themes across WordPress.

The second method which is more appropriate for custom skins is via the skin.php file which allows developers to add functions that are crucial to the skins functionality. Perhaps your going to build a skin that requires a custom post type for an FAQ page, in this case it would make sense to create a custom post type and use the Thesis 2.x query box to display posts specific to the FAQ page. Before we begin we first need to create a custom post type.

Creating a custom post type with custom.php

Add this to your custom.php file.

function faq_custom_init() {
      $labels = array(
          'name' => 'FAQ',
          'singular_name' => 'FAQ',
          'add_new' => 'Add New FAQ',
          'add_new_item' => 'Add New FAQ',
          'edit_item' => 'Edit FAQ',
          'new_item' => 'New FAQ',
          'all_items' => 'All FAQ',
          'view_item' => 'View FAQ',
          'search_items' => 'Search FAQ',
          'not_found' =>  'No FAQ found',
          'not_found_in_trash' => 'No FAQ found in Trash', 
          'parent_item_colon' => '',
          'menu_name' => 'FAQ',
          );

      $args = array(
          'labels' => $labels,
          'exclude_from_search' => false,
          'public' => true,
          'publicly_queryable' => true,
          'show_ui' => true, 
          'show_in_menu' => true, 
          'query_var' => true,
          'rewrite' => array( 'slug' => 'FAQ' ),
          'capability_type' => 'post',
          'has_archive' => false, 
          'hierarchical' => false,
          'taxonomies' => array('category'),
          'menu_position' => null,
          'supports' => array( 'title', 'author', 'thumbnail', 'excerpt', 'editor' )
          ); 

        register_post_type( 'faq', $args );
    }
    add_action( 'init', 'faq_custom_init', 0);

Now when you reload you WordPress admin panel you should see a new post type labeled FAQ.

faq custom post type menu

Now that you have your custom post type you can head over to the HTML editor.  Create a template for your FAQ page and instead of using a post box use a Query box and select your new post type.

thesis 2.1 query box

As you can see in this example I have created a few other custom post types to fulfill the needs of the skin.

You can of course customize the loop further but I will leave that up to you.

Adding Custom Post Types to Skin.php

Although the eventual results are the same there is a slight difference in adding your custom post type to custom.php and skin.php.

The code is almost the same with one difference. You need to add the action hook to the constructor function.

When you open your skin.php file you will see the following:

<?php
/*
Name: Skin Name
Author: Skin Author
Description: Skin Description
Version: version number e.g. 1.0
Class: skin_class
*/
class skin_class extends thesis_skin {

}

You will need to include another function inside the skins class.

<?php
/*
Name: Skin Name
Author: Skin Author
Description: Skin Description
Version: version number e.g. 1.0
Class: skin_class
*/
class skin_class extends thesis_skin {

    protected function construct() {

    }

}

Once you have added your construct function you can then add you custom post type below.

<?php
/*
Name: Skin Name
Author: Skin Author
Description: Skin Description
Version: version number e.g. 1.0
Class: skin_class
*/
class skin_class extends thesis_skin {

    protected function construct() {

    }

	/* Create FAQ custom post type */
    function faq_custom_init() {
      $labels = array(
          'name' => 'FAQ',
          'singular_name' => 'FAQ',
          'add_new' => 'Add New FAQ',
          'add_new_item' => 'Add New FAQ',
          'edit_item' => 'Edit FAQ',
          'new_item' => 'New FAQ',
          'all_items' => 'All FAQ',
          'view_item' => 'View FAQ',
          'search_items' => 'Search FAQ',
          'not_found' =>  'No FAQ found',
          'not_found_in_trash' => 'No FAQ found in Trash', 
          'parent_item_colon' => '',
          'menu_name' => 'FAQ',
          );

      $args = array(
          'labels' => $labels,
          'exclude_from_search' => false,
          'public' => true,
          'publicly_queryable' => true,
          'show_ui' => true, 
          'show_in_menu' => true, 
          'query_var' => true,
          'rewrite' => array( 'slug' => 'FAQ' ),
          'capability_type' => 'post',
          'has_archive' => false, 
          'hierarchical' => false,
          'taxonomies' => array('category'),
          'menu_position' => null,
          'supports' => array( 'title', 'author', 'thumbnail', 'excerpt', 'editor' )
          ); 

        register_post_type( 'faq', $args );
    }

}

Now notice that the add_action hook is missing, that is because we need to add it to the construct function.

    protected function construct() {
	    // construct custom post types
        add_action( 'init', array( $this,'faq_custom_init' ), 0);
    }

Once you have added it to your construct function you should now see your custom post type within the WordPress admin page and once again you will also be able to use the Thesis query box to display your custom post types.

Note: Notice the 0 on the end of our add action – this is to ensure that it fires early enough for Thesis to register it and automatically generate a template for you. In the above example the template will be found under single.

Meet the Author

Matthew Horne

Matthew Horne is web developer who specializes in optimized development. He also builds custom solutions instead of reverting to plugins. Matthew Has a strong understanding of PHP, JavaScript, jQuery.

8 comments… add one
  • carlo Nov 25, 2013, 10:43 am

    Hi

    as far as i know when you use a plugin like wp-types it is very simple to create custom post types and thesis 2.1 auto creates a template for this…

    • Matthew Horne Nov 25, 2013, 11:08 am

      That is true, but if you are going to include certain custom templates in a skin and advertise as such you cannot expect people to follow a set of instructs to set up their template. For users extending an existing skin it would help, however for developers is would be bad practice.

      • Pedro Lima Dec 19, 2013, 7:50 pm

        Could you elaborate on that, not sure I really got that one Matt. What is bad practice for dev, to use this tutorial or use the method Carlo mentioned?

        • Matthew Horne Dec 21, 2013, 4:30 am

          Its bad practice as a developer to assume a users of your products knows how to do everything. When and where possible, a developer should always make it simple.

          Because we can include any custom post types required directly as part of our themes or Skins – we should do that. A plugin is a 3rd party extension which means you then become reliant on it, what happens if the plugin stops getting supported?

          For Thesis add any required custom post types to skin.php

  • Ian Jan 26, 2014, 10:50 am

    This is a great post Mat, very informative! I’m a dab hand with Types/Views but as you quite rightly point out this is a much easier way of attacking custom posts with skins.

    If I want to add more CPT’s do I have to repeat the above process (skin method) or can I just adjust the construct function, like so:

    protected function construct() {
    // construct custom post types
    add_action( ‘init’, array( $this,’faq_custom_init’ ), 0);
    add_action( ‘init’, array( $this,’ANOTHER_CUSTOM_POST_init’), 0);
    }

  • Quint Apr 25, 2014, 11:52 pm

    Hello Matthew,

    When I add the Query Box, I do not see the “Options” tab. Do you know why that would be? Thanks!

    • Matthew Horne Apr 26, 2014, 5:24 am

      You will see them if you either install the developers box and enable box options or look in skin content.

Leave a Comment