0

I have a child theme with two custom post types 'jazz clubs' and 'jazz festival'. I would like the single.php to either display different content dependent on whether the post is from jazz clubs or jazz festival.

The problem is, how can I create this using single.php file. Have tried creating single-jazz-clubs.php and single-jazz-festival.php but the current single.php keeps loading the parent content.

Actually I can <?php get_template_part( 'content', 'club' ); ?> from the single.php but is there a way of making sure it can show festival CPT, the post belongs to that post type.

The current single.php look like this:

get_header(); ?>
<div id="content" class="large-8 columns" role="main">
        <?php while ( have_posts() ) : the_post(); ?>
            <?php get_template_part( 'content', get_post_format() ); ?>s
            <nav class="nav-single">
                <span class="nav-previous"><?php previous_post_link( '%link', '<span class="meta-nav">' . _x( '&laquo;', 'Previous post link', 'wpforge' ) . '</span> %title' ); ?></span>
                <span class="nav-next"><?php next_post_link( '%link', '%title <span class="meta-nav">' . _x( '&raquo;', 'Next post link', 'wpforge' ) . '</span>' ); ?></span>
            </nav><!-- .nav-single -->

            <?php comments_template( '', true ); ?>

        <?php endwhile; // end of the loop. ?>

</div><!-- #content -->

And here are my CPT's:

function jazz_clubs() {

$labels = array(
    'name'                => _x( 'Jazz Clubs', 'Post Type General Name', 'text_domain' ),
    'singular_name'       => _x( 'Jazz Club', 'Post Type Singular Name', 'text_domain' ),
    'menu_name'           => __( 'Jazz Clubs', 'text_domain' ),
    'parent_item_colon'   => __( 'Parent Jazz Clubs:', 'text_domain' ),
    'all_items'           => __( 'All Jazz Clubs', 'text_domain' ),
    'view_item'           => __( 'View Jazz Clubs', 'text_domain' ),
    'add_new_item'        => __( 'Add New Jazz Club', 'text_domain' ),
    'add_new'             => __( 'New Jazz Club', 'text_domain' ),
    'edit_item'           => __( 'Edit Jazz Club', 'text_domain' ),
    'update_item'         => __( 'Update Jazz Club', 'text_domain' ),
    'search_items'        => __( 'Search Jazz Club', 'text_domain' ),
    'not_found'           => __( 'No jazz club found', 'text_domain' ),
    'not_found_in_trash'  => __( 'No jazz clubs found in Trash', 'text_domain' ),
);
$args = array(
    'label'               => __( 'jazz club', 'text_domain' ),
    'description'         => __( 'Jazz Clubs around the world', 'text_domain' ),
    'labels'              => $labels,
    'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'trackbacks', 'revisions','post-formats' ),
    'taxonomies'          => array( 'region', 'country', 'city', 'state', 'post_tag' ),
    'hierarchical'        => false,
    'public'              => true,
    'show_ui'             => true,
    'show_in_menu'        => true,
    'show_in_nav_menus'   => true,
    'show_in_admin_bar'   => true,
    'menu_position'       => 5,
    'menu_icon'           => '/images/16-logo.png', // 16px16
    'can_export'          => true,
    'has_archive'         => true,
    'exclude_from_search' => false,
    'publicly_queryable'  => true,
    'query_var'           => 'jazz clubs',
    'capability_type'     => 'page',
);
register_post_type( 'jazz clubs', $args );

}
add_action( 'init', 'jazz_clubs', 0 );
?>

function jazz_festival() {

$labels = array(
    'name'                => _x( 'Jazz Festivals', 'Post Type General Name', 'text_domain' ),
    'singular_name'       => _x( 'Jazz Festival', 'Post Type Singular Name', 'text_domain' ),
    'menu_name'           => __( 'Jazz Festivals', 'text_domain' ),
    'parent_item_colon'   => __( 'Parent Jazz Festivals:', 'text_domain' ),
    'all_items'           => __( 'All Jazz Festivals', 'text_domain' ),
    'view_item'           => __( 'View Jazz Festivals', 'text_domain' ),
    'add_new_item'        => __( 'Add New Jazz Festivals', 'text_domain' ),
    'add_new'             => __( 'New Jazz Festival', 'text_domain' ),
    'edit_item'           => __( 'Edit Jazz Festivals', 'text_domain' ),
    'update_item'         => __( 'Update Jazz Festival', 'text_domain' ),
    'search_items'        => __( 'Search Jazz Festival', 'text_domain' ),
    'not_found'           => __( 'No jazz festivals found', 'text_domain' ),
    'not_found_in_trash'  => __( 'No jazz festivals found in Trash', 'text_domain' ),
);
$args = array(
    'label'               => __( 'jazz festival', 'text_domain' ),
    'description'         => __( 'Jazz Festivals around the world', 'text_domain' ),
    'labels'              => $labels,
    'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'trackbacks', 'revisions', 'post-formats', ),
    'taxonomies'          => array( 'region', 'country', 'city', 'months', 'musicians', 'post_tag'),
    'hierarchical'        => false,
    'public'              => true,
    'show_ui'             => true,
    'show_in_menu'        => true,
    'show_in_nav_menus'   => true,
    'show_in_admin_bar'   => true,
    'menu_position'       => 5,
    'menu_icon'           => '/images/16-logo.png',
    'can_export'          => true,
    'has_archive'         => true,
    'exclude_from_search' => false,
    'publicly_queryable'  => true,
    'query_var'           => 'jazz festival',
    'capability_type'     => 'page',
);
register_post_type( 'jazz festival', $args );

}

add_action( 'init', 'jazz_festival', 0 );
?>

2 Answers 2

0

Please check the post type slug. I think that is different than jazz-festival.

If you create single-POST_TYPE_SLUG.php it will work.

7
  • just added the cpt's for you
    – JCJ
    Jan 15, 2014 at 19:24
  • The post type key has space on it .. It may be the problem . If you don't have much posts.. Change register_post_type( 'jazz festival', $args ); To register_post_type( 'jazz_festival', $args ); And then create single-jazz_festival.php Jan 15, 2014 at 19:36
  • getting 404's now...what else needs to change? the posts are obviously still in the database but not displaying. Have also renamed the festival and club pages accordingly.
    – JCJ
    Jan 15, 2014 at 19:57
  • Add new post & test if it works.. Also you can try changing permalink.. Jan 15, 2014 at 20:09
  • ok have tested this with jazz clubs and festivals... it's loading the single-jazz_clubs.php and single-festival.php but what about content.php now. I have created content-jazz_festival.php and content-jazz_clubs.php but they aren't being rendered.
    – JCJ
    Jan 15, 2014 at 21:01
0

My solution for this same situation is the following

  • Maintain integrity of single.php
  • enhance it to allow custom post types to use the same file, yet have custom content displayed, different classes, etc.

Becomes

<?php
$post = get_queried_object();
if ( 'post' == $post->post_type ) {
    $post_format = get_post_format();
} else {
    $post_format = $post->post_type;
}
get_template_part( 'content', $post_format ); ?>

For posts, this will allow you to support post formats in your theme. This will also allow single.php to use content-jazz-festival.php as it's template part, rather than the default content.php. You can also specify other prefixes, as I am sure you know.

get_template_part( 'content-single', $post_format );

This would use content-single-jazz-festival.php with a fallback of content-single.php.

Full code:

<?php
global $post;
get_header(); ?>


<div id="content" class="large-8 columns" role="main">
        <?php while ( have_posts() ) : the_post(); ?>
            <?php
            if ( 'post' == $post->post_type ) {
                $post_format = get_post_format();
            } else {
                $post_format = $post->post_type;
            }?>
            <?php get_template_part( 'content', $post_format ); ?>
            <nav class="nav-single">
                <span class="nav-previous"><?php previous_post_link( '%link', '<span class="meta-nav">' . _x( '&laquo;', 'Previous post link', 'wpforge' ) . '</span> %title' ); ?></span>
                <span class="nav-next"><?php next_post_link( '%link', '%title <span class="meta-nav">' . _x( '&raquo;', 'Next post link', 'wpforge' ) . '</span>' ); ?></span>
            </nav><!-- .nav-single -->

            <?php comments_template( '', true ); ?>

        <?php endwhile; // end of the loop. ?>

</div><!-- #content -->
3
  • Actually like this approach...will try it and feedback Eric.
    – JCJ
    Jan 15, 2014 at 20:04
  • Just so I get this right... <?php $post = get_queried_object(); if ( 'post' == $post->jazz_club ) { $post_format = get_post_format(); } else { $post_format = $post->jazz_festival; } get_template_part( 'content', $post_format ); ?> <?php while ( have_posts() ) : the_post(); ?> <?php get_template_part( 'content-single', $post_format ); ?>
    – JCJ
    Jan 15, 2014 at 20:11
  • No, what I had was the complete code bit. I've added the full code to my answer. Depending on whether this is in single, or an archive page, the location you'd put it is different. This will work in both cases. Jan 16, 2014 at 2:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.