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

In the previous 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

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.

[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 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’ ) ) {

$args = array(
‘theme_location’ => ‘tertiary’,
‘container’ => ”,
‘menu_class’ => genesis_get_option(‘subnav_superfish’) ? ‘nav superfish’ : ‘nav’,
‘echo’ => 0
);

$subsubnav = wp_nav_menu( $args );

}
$subsubnav_output = sprintf( ‘<div id="subsubnav">%2$s%1$s%3$s</div>’, $subsubnav , genesis_structural_wrap( ‘subsubnav’, ‘<div class="wrap">’, 0 ) , genesis_structural_wrap( ‘subsubnav’, ‘</div><!– end .wrap –>’ , 0 ) );
echo apply_filters( ‘wps_do_subsubnav’, $subsubnav_output , $subsubnav , $args );
}[/php]

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.
Menu Screen Options 1

Menu Screen Options 2

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’ );

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

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.

[php]<?php
remove_action( ‘genesis_post_content’, ‘genesis_do_post_content’ );
add_action( ‘genesis_post_content’, ‘wps_do_diskbrakes_content’ );
function wps_do_diskbrakes_content() {
global $post;

//setup thumbnail image args to be used with genesis_get_image();
$size = ‘full’; // Change this to whatever add_image_size you want
$default_attr = array(
‘class’ => "aligncenter attachment-$size",
‘alt’ => $post->post_title,
‘title’ => $post->post_title,
);

// check if the post has a Post Thumbnail assigned to it. You can delete the if conditional if you want and assume that there will always be a thumbnail
if ( has_post_thumbnail() ) {
echo ‘<div class="clear"></div>’;
printf( ‘<a href="%s" title="%s">%s</a>’, get_permalink(), the_title_attribute( ‘echo=0’ ), genesis_get_image( array( ‘size’ => $size, ‘attr’ => $default_attr ) ) );
}
//else {} // you can add a default thumbnail conditional here if you want.

// display features from the metabox and custom fields
echo "<div class=’discbrakes-features’>";
echo "<h3>Features</h3>";
echo "<ul>";
echo "<li><span class=’feature-title’>Part Number:</span> <span class=’feature’>" . genesis_get_custom_field( ‘_wps_part_number’ ) . "</span></li>";
echo "<li><span class=’feature-title’>Rotor:</span> <span class=’feature’>" . genesis_get_custom_field( ‘_wps_rotor’ ) . "</span></li>";
echo "<li><span class=’feature-title’>Caliper:</span> <span class=’feature’>" . genesis_get_custom_field( ‘_wps_caliper’ ) . "</span></li>";
echo "<li><span class=’feature-title’>Mounting Brake:</span> <span class=’feature’>" . genesis_get_custom_field( ‘_wps_mounting_bracket’ ) . "</span></li>";
echo "<li><span class=’feature-title’>Weight/Axle Set:</span> <span class=’feature’>" . genesis_get_custom_field( ‘_wps_weight_per_axle_set’ ) . "</span></li>";
echo "</ul>";
echo "</div>";

// Display the content, if the content editor has content
if( $post->post_content != "" ) {
echo "<div class=’discbrakes-description’>";
echo "<h3>Description</h3>";
the_content( __( ‘[Read more…]’ , ‘genesis’ ) );
echo "</div>";
}
}
[/php]

E. Include the Genesis Framework **VERY IMPORTANT

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

The next step would be to style the single page template accordingly and as needed.

So our finished product is this: single-wps_axlesizes.php
[php]
<?php

/**
* Single CPT Template: wps_axlesizes
*
* This file is responsible for the display of
* single posts of the custom post type wps_axlesizes.
*
* @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_post_content’, ‘genesis_do_post_content’ );
add_action( ‘genesis_post_content’, ‘wps_do_diskbrakes_content’ );
function wps_do_diskbrakes_content() {
global $post;

//setup thumbnail image args to be used with genesis_get_image();
$size = ‘full’; // Change this to whatever add_image_size you want
$default_attr = array(
‘class’ => "aligncenter attachment-$size",
‘alt’ => $post->post_title,
‘title’ => $post->post_title,
);

// check if the post has a Post Thumbnail assigned to it. You can delete the if conditional if you want and assume that there will always be a thumbnail
if ( has_post_thumbnail() ) {
echo ‘<div class="clear"></div>’;
printf( ‘<a href="%s" title="%s">%s</a>’, get_permalink(), the_title_attribute( ‘echo=0’ ), genesis_get_image( array( ‘size’ => $size, ‘attr’ => $default_attr ) ) );
}
//else {} // you can add a default thumbnail conditional here if you want.

// display features from the metabox and custom fields
echo "<div class=’discbrakes-features’>";
echo "<h3>Features</h3>";
echo "<ul>";
echo "<li><span class=’feature-title’>Part Number:</span> <span class=’feature’>" . genesis_get_custom_field( ‘_wps_part_number’ ) . "</span></li>";
echo "<li><span class=’feature-title’>Rotor:</span> <span class=’feature’>" . genesis_get_custom_field( ‘_wps_rotor’ ) . "</span></li>";
echo "<li><span class=’feature-title’>Caliper:</span> <span class=’feature’>" . genesis_get_custom_field( ‘_wps_caliper’ ) . "</span></li>";
echo "<li><span class=’feature-title’>Mounting Brake:</span> <span class=’feature’>" . genesis_get_custom_field( ‘_wps_mounting_bracket’ ) . "</span></li>";
echo "<li><span class=’feature-title’>Weight/Axle Set:</span> <span class=’feature’>" . genesis_get_custom_field( ‘_wps_weight_per_axle_set’ ) . "</span></li>";
echo "</ul>";
echo "</div>";

// Display the content, if the content editor has content
if( $post->post_content != "" ) {
echo "<div class=’discbrakes-description’>";
echo "<h3>Description</h3>";
the_content( __( ‘[Read more…]’ , ‘genesis’ ) );
echo "</div>";
}
}

genesis();
[/php]

What can I do for you!?

Custom Development

We develop plugins by determining both business/functional and technical requirements, following WordPress development best practices, and using agile methodology to ensure you get the best solution.

Consulting

Have questions? Need a reliable developer to consult? Please contact us today!

Customized Theme

We can customize your theme or child theme, or create a child theme for you based on your needs while enhancing the performance of every individual attribute.

Customized Plugin

We can customize your plugins, extend plugins (e.g., Gravity Forms, Jetpack, Soliloquy) based on your needs ensuring security, performance, and positive business impact.

Comments

  1. Bruce says

    Hi Travis,
    Very nice explanation. Never thought about manually activating the menu only for the CPT:

    // 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;

    Very nice write up!
    }

  2. says

    I’m using Part B of this tutorial to create a third menu to the WordPress Menu System to work in conjunction with the two already there. What call do I use to actually display it like the other two in Genesis?

  3. says

    Excellent Tutorial,
    If the mounting brake field is left empty in the backend, How can I get it to not display the Mounting Brake: on the frontend. Hopes this makes since..

    Thanks

    Mike

    • says

      Hello Mike,

      To dynamically hide fields, you would modify the output like this (modifying lines 26-30):

      [php]<?php
      if ( genesis_get_custom_field( ‘_wps_part_number’ ) )
      echo "<li><span class=’feature-title’>Part Number:</span> <span class=’feature’>" . genesis_get_custom_field( ‘_wps_part_number’ ) . "</span></li>";
      if ( genesis_get_custom_field( ‘_wps_rotor’ ) )
      echo "<li><span class=’feature-title’>Rotor:</span> <span class=’feature’>" . genesis_get_custom_field( ‘_wps_rotor’ ) . "</span></li>";
      if ( genesis_get_custom_field( ‘_wps_caliper’ ) )
      echo "<li><span class=’feature-title’>Caliper:</span> <span class=’feature’>" . genesis_get_custom_field( ‘_wps_caliper’ ) . "</span></li>";
      if ( genesis_get_custom_field( ‘_wps_mounting_bracket’ ) )
      echo "<li><span class=’feature-title’>Mounting Brake:</span> <span class=’feature’>" . genesis_get_custom_field( ‘_wps_mounting_bracket’ ) . "</span></li>";
      if ( genesis_get_custom_field( ‘_wps_weight_per_axle_set’ ) )
      echo "<li><span class=’feature-title’>Weight/Axle Set:</span> <span class=’feature’>" . genesis_get_custom_field( ‘_wps_weight_per_axle_set’ ) . "</span></li>";
      [/php]

  4. Brooke says

    I’m confused about how you started out as single-wps_discbrakes.php and then ended up with single-wps_axlesizes.php in the finished product.

Leave a Reply