Archived topic
Conditional in the content-page.php file
7 replies · Started by Dan on July 15, 2020
Hi
I would like to show the page title only when using the default (page.php) template. For other page templates used in the site the title should be hidden.
I created this conditional in the content-page.php file but it is not working this code below hides the title for all page templates.
if (is_page_template('page.php')){
the_title( '<h1 class="entry-title" itemprop="headline">', '</h1>' );
}
Any ideas why?
Thanks,
Dan
Hi there,
simpler way is to use a Layout Element:
https://docs.generatepress.com/article/layout-element-overview/
You can disable the Content Title.
Set Display Rules to Entire Site and Exclude Pages.
Thanks David, but there is no option to work with page templates in the layout element.
Would you know what would be the way to make it work with the condition?
Thanks
Dan
I found a solution that works:
if(basename(get_page_template()) === 'page.php'){
the_title( '<h1 class="entry-title" itemprop="headline">', '</h1>' );
}
I don't know why the previous code didn't work but this does.
adding 'basename' made it work.
Dan
That's strange, your original code should definitely work as long as nothing is interfering with it.
The only thing I can think about is that WP isn't recognizing page.php as the default template and therefore ignoring the condition.
Ah, that could be it. It's possible that is_page_template() looks for custom page templates as opposed to default ones.
Correct. That's what I read here:
https://wordpress.stackexchange.com/questions/149803/conditional-tag-to-check-if-page-php-is-being-used/239950#239950
Didn't know that.