Site icon WP Smith

Developing a Custom Post Type and a Custom Taxonomy in Genesis with Custom Pages, Part 3

In the first tutorial, Developing a Custom Post Type and a Custom Taxonomy in Genesis with Custom Pages, Part 1, we covered the following:

  1. Register your Custom Post Type
  2. Register your Custom Taxonomy
  3. Create my Metaboxes
  4. Clear Permalinks

In the previous tutorial Developing a Custom Post Type and a Custom Taxonomy in Genesis with Custom Pages, Part 2, we covered how to Create the Single Page Template for the Custom Post Type.

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.)

[php]<?php
// Place the secondary navigation menu below the title
remove_action( 'genesis_after_header', 'genesis_do_subnav' );
add_action( 'genesis_after_post_title', 'genesis_do_subnav' );

// Enable the secondary navigation menu for single post type
add_filter('genesis_options', 'wps_define_genesis_setting' , 10, 2);
function wps_define_genesis_setting( $options, $setting ) {
if( $setting == GENESIS_SETTINGS_FIELD ) {
$options['subnav'] = 1;
}
return $options;
}
[/php]

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.

C. Post Meta/Post Info

Now, I would like to remove the post meta and post info. (**This assumes that your site hasn't done anything with Secondary Navigation.
[php]<?php
// Remove the post info function
remove_action( 'genesis_before_post_content', 'genesis_post_info' );

// Remove the post meta function
remove_action( 'genesis_after_post_content', 'genesis_post_meta' );
[/php]

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.

[php]<?php
remove_action('genesis_loop', 'genesis_do_loop');
add_action('genesis_loop', 'wps_do_axlesizes_loop');
function wps_do_axlesizes_loop() {
global $query_args; // any wp_query() args

// Get term
$term = get_query_var( 'term' );

$args = array(
'tax_query' => array(
array(
'taxonomy' => 'wps_axlesizes',
'field' => 'slug',
'terms' => $term
)
)
);

genesis_custom_loop( wp_parse_args( $query_args , $args ) );
}[/php]

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.

[php]<?php
remove_action('genesis_post_content', 'genesis_do_post_content');
add_action('genesis_post_content', 'wps_do_axlesizes_content');
function wps_do_axlesizes_content() {
global $post;

$size = 'thumbnail'; // Change this to whatever add_image_size you want
$default_attr = array(
'class' => "alignleft attachment-$size",
'alt' => $post->post_title,
'title' => $post->post_title,
);

// check if the post has a Post Thumbnail assigned to it.
if ( has_post_thumbnail() ) {
printf( '<a href="%s" title="%s">%s</a>', get_permalink(), the_title_attribute( 'echo=0' ), genesis_get_image( array( 'size' => $size, 'attr' => $default_attr ) ) );
}

the_content( __( '[Read more...]' , 'genesis' ) );

printf( '<a href="%s" title="%s">%s</a>', get_permalink(), the_title_attribute( 'echo=0' ), __( 'click for specifications' ) );

echo '<div class="clear"></div>';
}[/php]

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.

So our finished product is this: single-wps_axlesizes.php
[php]
<?php
/**
* Taxonomy Template: wps_axlesizes
*
* This file is responsible for the display of
* taxonomy archives for wps_axlesizes custom taxonomy.
*
* @author Travis Smith <travis@wpsmith.net>
* @copyright Copyright (c) 2011, Travis Smith
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
*
*/

// Place the secondary navigation menu below the title
remove_action( 'genesis_after_header', 'genesis_do_subnav' );
add_action( 'genesis_after_post_title', 'genesis_do_subnav' );

// Enable the secondary navigation menu for single post type
add_filter('genesis_options', 'wps_define_genesis_setting' , 10, 2);
function wps_define_genesis_setting( $options, $setting ) {
if( $setting == GENESIS_SETTINGS_FIELD ) {
$options['subnav'] = 1;
}
return $options;
}

// Remove the post info function
remove_action( 'genesis_before_post_content', 'genesis_post_info' );

// Remove the post meta function
remove_action( 'genesis_after_post_content', 'genesis_post_meta' );

remove_action('genesis_loop', 'genesis_do_loop');
add_action('genesis_loop', 'wps_do_axlesizes_loop');
function wps_do_axlesizes_loop() {
global $query_args; // any wp_query() args

// Get term
$term = get_query_var( 'term' );

$args = array(
'tax_query' => array(
array(
'taxonomy' => 'wps_axlesizes',
'field' => 'slug',
'terms' => $term
)
)
);

genesis_custom_loop( wp_parse_args( $query_args , $args ) );
}

remove_action('genesis_post_content', 'genesis_do_post_content');
add_action('genesis_post_content', 'wps_do_axlesizes_content');
function wps_do_axlesizes_content() {
global $post;

$size = 'thumbnail'; // Change this to whatever add_image_size you want
$default_attr = array(
'class' => "alignleft attachment-$size",
'alt' => $post->post_title,
'title' => $post->post_title,
);

// check if the post has a Post Thumbnail assigned to it.
if ( has_post_thumbnail() ) {
printf( '<a href="%s" title="%s">%s</a>', get_permalink(), the_title_attribute( 'echo=0' ), genesis_get_image( array( 'size' => $size, 'attr' => $default_attr ) ) );
}

the_content( __( '[Read more...]' , 'genesis' ) );

printf( '<a href="%s" title="%s">%s</a>', get_permalink(), the_title_attribute( 'echo=0' ), __( 'click for specifications' ) );

echo '<div class="clear"></div>';
}

add_action( 'genesis_before_content' , 'rdwd_page_title' , 10 );
function rdwd_page_title() {
?>
<h1>Disc Brake Kits</h1>
<?php
}

genesis();
[/php]