自定义帖子类型`if(array_key_exists` else查询

时间:2017-10-21 18:41:54

标签: php arrays wordpress if-statement

我已配置custom_post_type以使用

显示多个页面
function discovr_campaign_endpoints() {
add_rewrite_endpoint( 'overview', EP_PERMALINK );
add_rewrite_endpoint( 'new-campaign-details', EP_PERMALINK );
add_rewrite_endpoint( 'new-campaign-audience', EP_PERMALINK );
add_rewrite_endpoint( 'new-campaign-page', EP_PERMALINK );
add_rewrite_endpoint( 'new-campaign-ads', EP_PERMALINK );
add_rewrite_endpoint( 'edit-campaign', EP_PERMALINK );
add_rewrite_endpoint( 'analytics', EP_PERMALINK );
add_rewrite_endpoint( 'preview-campaign', EP_PERMALINK );
}

add_action( 'init', 'discovr_campaign_endpoints' );

并在single-post-type.php

<?php 
if( array_key_exists( 'overview', $wp_query->query_vars ) ){
    include("campaign-templates/single-campaign-overview.php");
} 
if( array_key_exists( 'new-campaign-details', $wp_query->query_vars ) ){
    include("campaign-templates/single-campaign-new-details.php");
}
if( array_key_exists( 'new-campaign-audience', $wp_query->query_vars ) ){
    include("campaign-templates/single-campaign-new-audience.php");
} 
if( array_key_exists( 'new-campaign-page', $wp_query->query_vars ) ){
    include("campaign-templates/single-campaign-new-page.php");
} 
if( array_key_exists( 'new-campaign-ads', $wp_query->query_vars ) ){
    include("campaign-templates/single-campaign-new-ads.php");
} 
if( array_key_exists( 'edit-campaign', $wp_query->query_vars ) ){
    include("campaign-templates/single-campaign-edit.php");
} 
if( array_key_exists( 'analytics', $wp_query->query_vars ) ){
    include("campaign-templates/single-campaign-analytics.php");
}
if( array_key_exists( 'preview-campaign', $wp_query->query_vars ) ){
    include("campaign-templates/single-campaign-preview.php");
}?>

根据每个endpoint

显示每个页面的相应内容

但是,我仍然希望显示

<?php the_content();?>

如果未在single-post-type.php页面中显示这些页面。

我尝试过使用

if( array_key_exists( 'new-campaign-details', $wp_query->query_vars ) )
{
    include("campaign-templates/single-campaign-new-details.php");
}else{
     echo the_content();
}
然而,每次

,这将多次显示the_content,我相信我需要一种方法在个别if(*first* array_key_exists)周围添加elseif( array_key_exists语句,以查询是否存在任何这些端点else} echo the_content();

就像下面的例子一样(这似乎有用,如果我替换接受echo 'Hello World';而不是the_content();

<?php 
if( array_key_exists( 'overview', $wp_query->query_vars ) ){
    include("campaign-templates/single-campaign-overview.php");
} 
elseif( array_key_exists( 'new-campaign-details', $wp_query->query_vars ) ){
    include("campaign-templates/single-campaign-new-details.php");
}
elseif( array_key_exists( 'new-campaign-audience', $wp_query->query_vars ) ){
    include("campaign-templates/single-campaign-new-audience.php");
} 
elseif( array_key_exists( 'new-campaign-page', $wp_query->query_vars ) ){
    include("campaign-templates/single-campaign-new-page.php");
} 
elseif( array_key_exists( 'new-campaign-ads', $wp_query->query_vars ) ){
    include("campaign-templates/single-campaign-new-ads.php");
} 
elseif( array_key_exists( 'edit-campaign', $wp_query->query_vars ) ){
    include("campaign-templates/single-campaign-edit.php");
} 
elseif( array_key_exists( 'analytics', $wp_query->query_vars ) ){
    include("campaign-templates/single-campaign-analytics.php");
}
elseif( array_key_exists( 'preview-campaign', $wp_query->query_vars ) ){
    include("campaign-templates/single-campaign-preview.php");
}
else{
    echo the_content();
}
?>

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

不用担心,我的解决方案是正确的,只是遗漏了一些关键点。

如果有其他人遇到此问题,请对此表示怀疑。来自全能者的愚蠢错误。

<?php 
if( array_key_exists( 'overview', $wp_query->query_vars ) ){

include("campaign-templates/single-campaign-overview.php");
} 
elseif( array_key_exists( 'new-campaign-details', $wp_query->query_vars ) ){
    include("campaign-templates/single-campaign-new-details.php");
}
elseif( array_key_exists( 'new-campaign-audience', $wp_query->query_vars ) ){
    include("campaign-templates/single-campaign-new-audience.php");
} 
elseif( array_key_exists( 'new-campaign-page', $wp_query->query_vars ) ){
    include("campaign-templates/single-campaign-new-page.php");
} 
elseif( array_key_exists( 'new-campaign-ads', $wp_query->query_vars ) ){
    include("campaign-templates/single-campaign-new-ads.php");
} 
elseif( array_key_exists( 'edit-campaign', $wp_query->query_vars ) ){
    include("campaign-templates/single-campaign-edit.php");
} 
elseif( array_key_exists( 'analytics', $wp_query->query_vars ) ){
    include("campaign-templates/single-campaign-analytics.php");
}
elseif( array_key_exists( 'preview-campaign', $wp_query->query_vars ) ){
    include("campaign-templates/single-campaign-preview.php");
}
else{
    get_header('new');
    if (have_posts()) : while (have_posts()) : the_post();
    the_content();
    endwhile;
    endif; 
    get_footer('new');
}
?>