简体   繁体   中英

Wordpress conditional tag to identify if custom header.php is used

I am looking for a solution to identifying ONLY ONE PAGE (4 in the list below) with conditional statement when using add_rewrite_endpoint on a custom post-type .

For example, let's say that I have four types of pages per the post-type campaigns ...

  1. campaigns/ post-title /overview
  2. campaigns/ post-title /preview
  3. campaigns/ post-title /analytics
  4. campaigns/ post-title - As single-campaigns.php

using this code in a function

add_rewrite_endpoint( 'overview', EP_PERMALINK );
add_rewrite_endpoint( 'analytics', EP_PERMALINK );
add_rewrite_endpoint( 'preview-campaign', EP_PERMALINK );

Then calling on the seperate page-templates in single-campaigns.php with

if( array_key_exists( 'overview', $wp_query->query_vars ) ){
include("campaign-templates/single-campaign-overview.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('landing');
        if (have_posts()) : while (have_posts()) : the_post();
        if ( $post->post_content=="" ) {
           the_content(); 
        } else {
           the_content(); 
        };
    endwhile;
    endif; 
    get_footer('new');
}

Obviously, the use of is_singular , is_single , etc will identify and apply the condition to all page types of each post.

My original idea was to add the condition to something to identifying the use of get_header(landing) , is this possible?

EDIT - To give more context. I am trying to apply the condition of if to only the single (#4 as shown in the list above) page and no others.

if ( **single-campaigns.php**() ) {
    //Function
}else{
    wp_register_script( 'wp_auth_check', '/wp-includes/js/wp-auth-check.js' , array('heartbeat'), false, 1);
...
}

You have all, what you needed. You created endpoints,create condition and check true for every single types. Create variables for template or header. And past to call get_header() or get_template_part(); May be something like this might help you

if ( array_key_exists( 'overview', $wp_query->query_vars ) ) {
    $template = 'overview';
    $header   = 'test';
} elseif ( array_key_exists( 'analytics', $wp_query->query_vars ) ) {
    $template = 'analytics';
    $header   = 'tes1';
} elseif ( array_key_exists( 'preview-campaign', $wp_query->query_vars ) ) {
    $template = 'preview-campaign';
    $header   = 'test2';
} else {
    $template = 'default';
    $header   = '';
}

...

get_header( $header );

...

get_template_part( '/parts/', $template );

Or create function to use in conditionals:

function is_single_with_endpoint( $endpoint ) {
    global $wp_query;

    if ( ! isset( $endpoint ) && ! empty( $endpoint ) ) {
        return false;
    }
    if ( array_key_exists( $endpoint, $wp_query->query_vars ) ) {
        return true;
    } else {
        return false;
    }
}

if ( is_single_with_endpoint( 'overview' ) ) {
    // get_header or something
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM