Is_page_template() Only returning false

Hi guys, I am having an issue with adding a sidebar in my custom template on wordpress. I have added the following code to my filters.php file.

add_filter('sage/display_sidebar', function ($display) {
    static $display;

    isset($display) || $display = in_array(true, [
        // The sidebar will be displayed if any of the following return true
        is_single(),
        is_404(),
        is_page_template('template-custom.php'),
    ]);

    return $display;
});

I have narrowed down the issue to the is_page_template function and found that it is constantly returning false regardless of whether or not I am on a custom template page. Any help would be much appreciated. Thanks!

Hi @SeanMee86,

Have you tried is_page_template('views/template-custom.blade.php')?

– Matt

3 Likes

To confirm @mmirus’s suggestion, try {!! get_page_template() !!} on the template itself. That’ll give you the name you need to check for.

2 Likes

Thank you guys so much for the quick replies, I could’ve sworn I had tried views/template-custom.blade.php before and it didn’t work, but now it does and the echoing get_page_template() will be very useful in the future. Thanks again!

2 Likes

Thanks @mmirus this works inside blade templates and in setup.php for enqueue scripts, but for some reason no matter what I tried it returns false inside an AJAX function. Maybe you have some ideas about that?

Quick thought without looking into it: assuming you’re using wp_ajax, would is_page_template() make sense in that context? After all, WordPress isn’t loading a page template, just taking your request to the AJAX URL and running the specified action. (I think) it’s not going through the whole process of matching the request to the template hierarchy and finding a template.

In that case, you’d either have to pass the info you need in your AJAX request, or look it up in your PHP using the post ID or something.

If that’s not it, more detail about your request (actual code if possible) could help narrow things down.

2 Likes

Yes you’re right, it didn’t make sense :slight_smile:
I’m saving get_page_template_slug() inside data attr and passing that as a var to ajax function to do necessary checks.
Thanks, for the tip.

1 Like