Custom Post Types & Thesis 2

Featured image of article: Custom Post Types & Thesis 2

I love to build WordPress websites, and I really love the Thesis framework. Thesis takes care of the heavy-lifting and allows you to focus on the style and functionality of a website.

When Thesis 2.0 came out last year, it was a big surprise – the framework changed almost beyond recognition. At first, change was painful. All those comforting little familiar nuances were gone.

I felt that after 3 years learning how to deploy WordPress sites using Thesis, it was back to the drawing board.

However I needn’t have worried – after a couple of weekends digging in to Thesis, I realized that the framework is more powerful than ever.

Custom Post Types & Thesis

Most recently, I needed to create WordPress custom post types. Thesis 2 handles this with ease – when you create a custom post type, Thesis 2 recognises this and automatically generates a HTML template for the new post type.

This template can be customised to your heart’s content, and the Thesis query box helpfully allows you to select posts, pages or your custom post types – giving you full control over how your custom post types are displayed.

Once my custom post types were set up, I needed to create a customised back-end, so that site administrators could easily add data. For example, in my “Person” custom post type, I needed it to be easy for administrators to add biographical data. In a similar way, I needed associated content to be linked – for example, the administrator should be able to select projects that the person has been involved with, and these should be displayed on that person’s page.

Once the backend has been built and content added, it isn’t immediately obvious how to display this data on the site using Thesis. By using Thesis hooks, you can create PHP functions to call the custom field data, and have this fire in the correct place in your page template.

Thesis Hooks: Custom Field Content

Here’s how you get custom fields into your Thesis page template:

  • Add the function that calls your custom field in the customs.php file of your active skin
  • Link to this to your page template by means of Thesis hooks

For example, to display the Advanced Custom Fields field “job_title” in a Thesis 2 template, add the following code to your skin’s custom.php. The “get_field” and “the_field” functions are part of the Advanced Custom Fields plugin API, and the slug for my custom field is “job_title”.

function add_job_title_field() {

  //Next line: don't run function if field is empty
  if (get_field('job_title')!= ""){

	  <h3 class="subheading"><?php the_field('job_title'); ?></h3>

add_action('thesis_hook_container_job_title_top', 'add_job_title_field');

You then add the hook “job_title” – as referenced in ‘thesis_hook_container_job_title_top’ – where you want the field to display. You do this by entering the hook name in the “Unique Hook Name” field of the container in question, as shown in the screenshot below:

Job Title Screenshot from 2013-08-07 17:17:36

I created empty containers that use hooks to fire the functions, and I can attach a class or ID to the container for styling purposes. Sorted!

This is a really useful function – made me realise how powerful Thesis 2 is…