$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Single Page for two custom post types|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Single Page for two custom post types

matteradmin10PV0评论

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

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 );
?>
Share Improve this question edited Jan 5, 2019 at 8:29 Sven 3,6841 gold badge36 silver badges48 bronze badges asked Jan 15, 2014 at 19:16 JCJJCJ 371 silver badge10 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 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

  • 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 -->

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far