samedi 21 octobre 2017

Custom post type `if( array_key_exists` else query

I have configured a custom_post_type to display multiple pages using

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

and within the 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");
}?>

to display the appropriate content for each page as per each endpoint

However, I would still like to display

<?php the_content();?>

if these pages are not being shown, in the single-post-type.php page.

I have tried using

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

on each, however, this then will display the_content multiple times and I believe I need a way to add an if statement around all the if( array_key_exists combined to query whether any of these endpoints exist else} echo the_content();

Thank you for your help.

Aucun commentaire:

Enregistrer un commentaire