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( '«', 'Previous post link', 'wpforge' ) . '</span> %title' ); ?></span>
<span class="nav-next"><?php next_post_link( '%link', '%title <span class="meta-nav">' . _x( '»', '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 );
?>
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( '«', 'Previous post link', 'wpforge' ) . '</span> %title' ); ?></span>
<span class="nav-next"><?php next_post_link( '%link', '%title <span class="meta-nav">' . _x( '»', '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 );
?>
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.
My solution for this same situation is the following
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( '«', 'Previous post link', 'wpforge' ) . '</span> %title' ); ?></span>
<span class="nav-next"><?php next_post_link( '%link', '%title <span class="meta-nav">' . _x( '»', 'Next post link', 'wpforge' ) . '</span>' ); ?></span>
</nav><!-- .nav-single -->
<?php comments_template( '', true ); ?>
<?php endwhile; // end of the loop. ?>
</div><!-- #content -->