0

i use the following code in my functions.php. I want to exclude all pages which use the single.php template.

function cat_menu() {

 if (!is_page_template('single.php')) {


$cur_cat = get_query_var('cat');

 $new_cats = wp_list_categories('echo=false&child_of=' . $cur_cat .'&depth=1&title_li=&&show_count=0&hide_empty=1');
            echo '<ul>' . $new_cats . '</ul>';
}}
  add_action ( 'genesis_after_header', 'cat_menu' ); 

But i see the result aslo on pages which use the single.php.

Can somebody help me with this?

4 Answers 4

1

is_page_template() checks against post type templates specified by the theme using

/**
 * Template Name: My Template
 */

at the top of the template file. Not any arbitrary template file. For this, you might wanna check out the answers over at Get name of the current template file. You could grab the code from the accepted answer there (get_current_template()) to do something when single.php === get_current_template().

0

You must to use the next function:

is_singular()

Is singular checks if current page is single.php.

Function reference

1
  • is_singular() doesn't 100% guarantee that single.php was loaded, as other templates in the hierarchy might be loaded before it.
    – swissspidy
    Commented Jan 25, 2017 at 13:05
0

It's always strange to me how long it takes WPSE threads to receive accurate answers, here is a basic example of how to use it from StackOverflow:

// if not specific page template
<?php if (!is_page_template('page-templates/all-sponsor-template.php')); ?>
    // Stuff
<?php endif; ?>

// if is specific page template
<?php if (is_page_template('page-templates/all-sponsor-template.php')); ?>
    // Stuff
<?php endif; ?>

Ref: https://stackoverflow.com/questions/58531957/if-is-page-template-not-working-in-header-php

-1

get_page_template_slug() - Returns only the page template

 global $post;    
    $template = get_page_template_slug($post->ID);         
    if ($template == 'page-template.php') {....}

Probably worth checking out easier

if(is_single()){....}
1
  • This is the same problem as with is_page_template(). get_page_template_slug() only checks for custom post templates (the ones with the /* Template Name: ... */ headers). Also, is_single() doesn't 100% guarantee that single.php was loaded, as other templates in the hierarchy might be loaded before it.
    – swissspidy
    Commented Jan 25, 2017 at 13:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.