Now, in Developing a Custom Post Type and a Custom Taxonomy in Genesis with Custom Pages, Part 1, we have the registration of the custom post type, custom taxonomy and the addition of the metaboxes. In Part 2, we discussed the Single Page Template for the custom post type. In Part 3, we discussed the Taxonomy Template for the custom taxonomy. For good measure, let’s create a basic page template to wrap this all up. This will create a front page of sorts for the custom taxonomy and the single custom post type pages.

A. File Setup

First, create the file as page-{whatever}.php (WordPress Codex). While you can use page-{slug}.php, this can cause some unexpected issues. So naming it page-{whatever}.php forces the user to assign it via the page templates area. So in our case, it will be page-brakes.php.

B. Template Name

The first thing you must enter with any page template is the Template Name.
[php]<?php
/*
*Template Name: Disc Brakes Template
*/
[/php]

B. Menu Move

Now, for this example, I want to use a menu system. So I will be using the secondary menu system to accomplish this, and I want this to appear below the title.

If you notice, I programmatically turn on the secondary menu. However, again, in Genesis > Theme Settings, site-wide, I have the secondary navigation system turned off. Now, for this page template, I have selectively turned it on.

If you use the secondary navigation for your site, simply register a new navigation system and then add it. See the previous tutorial for this information.

In this tutorial, we will walk through how to Create a Custom Taxonomy Page Template. Now the specifics will be different from custom taxonomy to custom taxonomy. This is how a taxonomy archives will appear, just like if someone clicks on a specific category or tag (via category.php). However, I will walk you through a basic setup.

A. File Setup

First, create the file as taxonomy-{taxonomy_registered_name}.php (WordPress Codex). So in our case, it will be single-wps_axlesizes.php.

B. Menu Move

Now, for this example, I want to use a menu system. So I will be using the secondary menu system to accomplish this, and I want this to appear below the title. (**This assumes that your site hasn’t done anything with Secondary Navigation.)

If you notice, I programmatically turn on the secondary menu. However, in Genesis > Theme Settings, sitewide, I have the secondary navigation system turned off. Now, for this page template, I have selectively turned it on.

If you already use the secondary navigation for your site, simply register a new additional navigation system and then add it. See the previous tutorial for this information.

D. Custom Loop **VERY IMPORTANT

Now, I want to create my custom display of the content via the loop. This section will dramatically change from custom taxonomy to custom taxonomy based on what type of information you’d like to display and how you’d like to display it. This would be where you would introduce the Genesis Grid Loop, if you wanted to go that route. Since this file refers to all the terms in the custom taxonomy, we have to code thinking about them all.

E. Custom Content **VERY IMPORTANT

Finally, I want to create my custom display of the content via the content. Again, this section will dramatically change from custom taxonomy to custom taxonomy based on what type of information you’d like to display and how you’d like to display it.

Now, for SEO purposes, we wanted to include the page name that will be associated with

tags.
// Add a page title (optional). This can be hard coded to pull the title from a specific page, but it is easier just to enter the page name here.
[php]<?php
add_action( ‘genesis_before_content’ , ‘wps_page_title’ , 10 );
function wps_page_title() {
?>
<h1>Disc Brake Kits</h1>
<?php
}
[/php]

F. Include the Genesis Framework **VERY IMPORTANT

[php]<?php
genesis();
[/php]

The next step would be to style the taxonomy archive accordingly and as needed, which I am not going to cover here.

Now that we have registered our custom post type and taxonomy and created our custom metabox, we will work on displaying them using a custom page template. In this tutorial, we will walk through how to Create the Single Page Template for the Custom Post Type. Note that this will be different from custom post type to custom post type. This is how a single post inside the custom post type will appear. However, I will walk you through a basic setup.

A. File Setup

First, create the file as single-{cpt_registered_name}.php (WordPress Codex). So in our case, it will be single-wps_discbrakes.php.

B. Menu Move

Now, for this example, I want to use a menu system. So I will be using the secondary menu system to accomplish this, and I want this to appear below the title.

If you notice, I have programmatically turn on the secondary menu. However, in Genesis > Theme Settings, sitewide, I have the secondary navigation system turned off. Now, for this page template, I have selectively turned it on.

If you use the secondary navigation for your site, simply register a new navigation system and then add it. First, you will need to add this code to functions.php:
[php]<?php
register_nav_menu( ‘tertiary’, ‘Tertiary Navigation Menu’ );
function wps_do_subsubnav() {
if ( has_nav_menu( ‘tertiary’ ) ) {

Since Genesis structural wraps do not natively extend beyond nav and subnav, you can use genesis_structural_wrap( 'subnav', '<div class="wrap">', 0 ) if you prefer. You should now see the third menu location in Appearance > Menus.

Now you may or may not (most likely not), see your new custom post types and custom taxonomies list on the left under pages and custom links.

To have your new custom post types and custom taxonomies lists appear click on screen options at the top right and select (check) the custom post type and custom taxonomy.

Then you would add the following to the single template:
[php]<?php
remove_action( ‘genesis_after_header’, ‘genesis_do_subnav’ );
add_action( ‘genesis_after_post_title’, ‘wps_do_subsubnav’ );
[/php]

C. POST META/POST INFO

Now, I would like to remove the post meta and post info.
[php]<?php
// Remove the post info function
remove_action( ‘genesis_before_post_content’, ‘genesis_post_info’ );

D. Custom Content **VERY IMPORTANT

Finally, I want to create my custom display of the content. This section will dramatically change from custom post type to custom post type based on what type of information you’d like to display and how you’d like to display it.

Recently, someone contacted me to develop a custom post type setup for their website, and they were lamenting how there is nothing on the web that succinctly walks them through the process: soup to nuts. So here’s my attempt! However, I am going to assume basic knowledge of custom post types. If you haven’t read through my Understanding Custom Post Types series yet, you may want to do that.

First, I created a new php file called wps-admin-functions.php. This is where we will house our Custom Post Types, Taxonomies, and Metaboxes. We could have added this to our theme’s function.php file but this will allow us to have a separate file dedicated to our newly added features. To do this, simply create a new text document in your favorite text editor and save as wps-admin-functions.php.

Notice in this code that I associate my custom post type with my coming custom taxonomy: 'taxonomies' => array( 'wps_axlesizes' ),. Also, note that I am using a prefix on both my custom post type and taxonomy. The reason this is done is that there may be a naming conflict with a plugin or other code in your theme. The prefix uniquely identifies it to prevent this. It is a best practice, and make sure you do not use the wp_ or genesis_ prefixes.

Once these are registered, you should see your custom post type to the left under POST.

3. Create my Metaboxes
With metaboxes, you can go the long way; however, I prefer to use a class. Soon WordPress will have a metabox class, hopefully in WordPress 3.3, so this section will be rendered “obsolete” yet still usable. First, download the custom metabox code from GitHub. For a great explanation of the code, see Bill Erickson’s tutorial. Because of the tutorial, I won’t break down this section.

Once downloaded, place the metabox folder in your lib folder in the child theme folder so that: CHILD_URL . '/lib/metabox/init.php'

4. Clear Permalinks
Now in your admin area, navigate to Settings > Permalinks and click Save Changes. This will flush and clear your permalinks. This is important if you are getting strange 404 Page Not Found errors.

So recently, someone asked me to help them assimilate two post types on the Genesis blog page. Since because of the Genesis loops, this becomes quite easy. So here’s what we need to do:

Setup the Template Name

Create the Custom Loop

Modify the Post Meta

Customize the Content Display

Setup the Template Name

First and as always, we need to set up the template. You can start by scratch, which this tutorial will do, or you can copy page_blog.php from the genesis folder.
[php]<?php
/**

Template Name: Blog

*/
[/php]

You may want to change the template name to something different so there aren’t any confusions with Genesis’s version. So something like this would work:
[php]<?php
/**

Template Name: Custom Blog

*/
[/php]

Create the Custom Loop

Now, I have a site with multiple custom post types and what I want to do is to combine two of those post types (post and wps_videos) into the blog feed. Then I want to limit them based on the Genesis category settings as well as incorporate the Video Category (wps_vidcats) in the same manner.

So first, I have to unhook the genesis standard loop.
[php]
remove_action(‘genesis_loop’, ‘genesis_do_loop’);
[/php]

These lines are right out of the Genesis standard loop. They pull the information set on the Theme Settings page and place them in variables to be used later. However, these are limited to just categories. Now, it is important to note that $include only contains one category id and $exclude contains an array of categories (even if there is only one).

So let me break this down for you. ‘showposts’ pulls from the Genesis Theme Settings page. ‘post_type’ is where we add the registered names of all the post types we would like to include in an array. ‘tax_query’ is where I limit the query based on my requirements. Now I wanted to only include posts from one category, which was set on the Genesis Theme Settings page, and I also wanted to do likewise with my videos and include videos from one specific Video Category (here term ID of 92). And since posts and videos do not share the same taxonomy, the tax_query is simple. So, in summary, I wanted posts from one category ($include) OR (and that’s my relation argument) posts from my custom taxonomy video category (here 92).

Now, we can finish the function by calling the Genesis Custom Loop.
[php]

genesis_custom_loop( $args );
}
[/php]

So all together the function looks like this (to simply limit posts to one category and vids from another category):
[php]<?php
remove_action(‘genesis_loop’, ‘genesis_do_loop’);
add_action( ‘genesis_loop’, ‘custom_do_loop’ , 5 );

Modify the Post Meta

Now, because my videos do not share the same taxonomy with posts (and it could have [dare I say, should have] but the client didn’t want it that way). My posts on the blog page that are from the videos custom post type won’t have any post meta. I could simply remove it, but since the client wanted it, I simply filter them.

About Travis

As a WordPress enthusiast, developer, and speaker, Travis writes about what he learns in WordPress trying to help other WordPress travelers, beginners and enthusiasts with tutorials, explanations, & demonstrations.