Roots Discourse

How to use is_page_template conditional tag in Sage 9 Beta 3

I am trying to use the conditional tag “is_page_template” in a Blade template that resides in partials. I’m using Sage 9 Beta 3, and I can’t figure out what to put in for the template file argument. Nothing I’ve tried has worked.
Ergo:
is_page_template(‘template-nofeat.blade.php’)
is_page_template(‘template-nofeat.php’)
is_page_template(‘nofeat.blade.php’)
is_page_template(‘nofeat.php’)
is_page_template(‘template-omfgwtf.php’)

None of that works. Just for giggles, I tried a plain is_page_template() with no argument and it worked in as much as it recognized the page does have a template applied, so I know my code is correct.

I can’t find any other posts with this question so I must be really not understanding how this Blade templating works. Can anyone help please?

3 Likes

If you var_dump get_page_template() you will see the page template name.

You could always do something like:

if(basename(get_page_template()) == "template-nofeat.blade.php")

1 Like

When I ran the var_dump, I just got the full path of the custom template. Tried the full path which didn’t work either.

This works, but I don’t feel like it’s optimal:

/**
 * Display sidebar
 */
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_page()
    ]);

    if (basename(get_page_template()) == "template-full-width.blade.php")
        $display = false;

    return $display;
});
3 Likes

Is there a better way to do that? I mean, it works but is like a dirty workaround :thinking:

I found a way guys.
Since you are in a custom page you can access to the post meta in the database and check the page template in this way
{{ get_post_meta( get_the_ID(), '_wp_page_template', TRUE ) }}
and this is returning the path of the page template.

Then if you do it:
{{ is_page_template('views/template-beautifulTemplateName.blade.php') }}
everything works :yum:

Tested in filters.php too:

3 Likes

Yeah this is a mess but it works :confused:

function display_sidebar()
{
    static $display;
    $get_page_template = basename(get_page_template());

    isset($display) || $display = in_array(true, [
        $get_page_template == 'page-sidebar.blade.php'
    ]);
    return $display;
}
2 Likes

I’m trying to get the primary sidebar to only appear on the main blog page and single post pages. I’ve got it working fine on the single post pages with ‘is_single()’, However can’t get the other blog/posts page to show the sidebar.

The page for the posts is ‘index.blade.php’ but on every page I visit after adding ‘{!! get_page_template() !!}’ , it seems to say ‘page.blade.php’ even when the page isn’t using ‘page.blade.php’.

I’ve tried:
is_page_template('views/index.blade.php')

is_page_template('index.blade.php')

is_page_template('views/page.blade.php')

function display_sidebar()
{
    static $display;
    $get_page_template = basename(get_page_template());

    isset($display) || $display = in_array(true, [
        $get_page_template == 'index.blade.php'
    ]);
    return $display;
}

Any help is much appreciated

Can you use is_home() ?

That worked, thanks for the quick reply