两种定制帖子类型的单一页面

时间:2014-01-15 作者:JCJ

我有一个儿童主题,有两个自定义帖子类型“爵士俱乐部”和“爵士音乐节”。我想要single.php 根据帖子是来自爵士俱乐部还是爵士音乐节,显示不同的内容。

问题是,如何使用single.php 文件已尝试创建single-jazz-clubs.phpsingle-jazz-festival.php 但是现在single.php 继续加载父内容。

事实上我可以<?php get_template_part( \'content\', \'club\' ); ?>single.php 但是有没有办法确保它可以显示节日CPT,这个帖子属于那种帖子类型。

当前single.php 如下所示:

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 -->
以下是我的CPT:

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 个回复
最合适的回答,由SO网友:Abdul Awal Uzzal 整理而成

请检查post type slug。我认为这与jazz-festival.

如果您创建single-POST_TYPE_SLUG.php 它会起作用的。

SO网友:Eric Holmes

对于这种情况,我的解决方案如下

维护单体的完整性。php增强了它,允许自定义帖子类型使用相同的文件,同时显示自定义内容、不同的类等。

变成

<?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 ); ?>
对于帖子,这将允许您在主题中支持帖子格式。这也将允许single.php 使用content-jazz-festival.php 因为它是模板部分,而不是默认部分content.php. 您也可以指定其他前缀,我相信您一定知道。

get_template_part( \'content-single\', $post_format );
这将使用content-single-jazz-festival.php 有一个后备content-single.php.

完整代码:

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

结束

相关推荐

Single.php没有流行起来

我正在将wordpress博客嵌入到html页面中,并按照步骤成功添加了我的网站www.srougi的页眉和页脚。商业/灵魂斗士/博客。但现在我需要把我的单曲风格化。php,即当用户单击文章标题时显示完整文章的页面。我在下面添加了一些命令,这些命令应该会带来自定义的页眉和页脚,因为我已经将页眉样式化了。php和页脚。php。但它完全没有配置,也没有风格。谁能帮帮我吗?非常感谢。看我的单曲。php代码:<?php global $OLDCAR_VAN; get_header();&#x