自定义 Post 类型的条件标签

Conditional Tags for Custom Post Types

我刚刚开始为我的网站使用自定义 post 类型,并且在该自定义 post 类型中的某些页面上,例如:

/custom/post-one /custom/post-three

我想在 header 中显示一些不同的内容。对于像这样的页面,这很容易做到:

if (is_page(array('page-here','page-here','page-here','page-here'))) {
include(TEMPLATEPATH.'/header-red.php');
} 
elseif (is_page(array('page-here','page-here','page-here','page-here'))) {
include(TEMPLATEPATH.'/header-blue.php');
}
elseif (is_page(array('page-here'))) {
include(TEMPLATEPATH.'/header-green.php');
}  
else {
get_header();
}

我将如何转换我用于普通页面的上述脚本,以在我的自定义 post 类型页面上工作?

您需要将此类代码用于 headr.php

if(is_singular( 'postType' )){
    //code which you want to render for this perticular post type   
}else{

    //your defalult code
}

在此代码中 postType 是您的 post 类型名称。

我现在通过使用 Post ID 实现了这一点,如下所示:

if ( get_the_ID() == 4915 ) {
include(TEMPLATEPATH.'/header-red.php');
} 
elseif ( get_the_ID() == 7564 ) {
include(TEMPLATEPATH.'/header-blue.php');
}
elseif ( get_the_ID() == 71564 ) {
include(TEMPLATEPATH.'/header-green.php');
}  
else {
get_header();
}