How can I make custom page templates work on child theme?

admin2025-06-02  3

I have created a page template called concert.php under page-templates directory with the following content:

<?php 
   /* Template Name: Concert*/

   add_body_classes('page_concert'); /* Custom function that adds arguments to
                                        body class using body_class filter */

   get_header();

   ...

   get_footer();

?>

The template shows up in the page editor as expected, however when loading a page that uses this template the default page.php template is used instead.

I've read in some threads that for a child theme to properly load page templates it is necessery to have a proper index.php, header.php and page.php, which I do have set in my theme folder. Also, some people have pointed out that the existence of a front-page.php leads to home page not loading its set template, however deleting the file from parent theme didn't do much for me.

The use of Template: theme-name inside the theme's style.css has said to be problematic, as well. But without that line of code the child theme becomes a standalone theme.

How can I make my custom page template work in a child theme? Has anyone been through the same situation?

I have created a page template called concert.php under page-templates directory with the following content:

<?php 
   /* Template Name: Concert*/

   add_body_classes('page_concert'); /* Custom function that adds arguments to
                                        body class using body_class filter */

   get_header();

   ...

   get_footer();

?>

The template shows up in the page editor as expected, however when loading a page that uses this template the default page.php template is used instead.

I've read in some threads that for a child theme to properly load page templates it is necessery to have a proper index.php, header.php and page.php, which I do have set in my theme folder. Also, some people have pointed out that the existence of a front-page.php leads to home page not loading its set template, however deleting the file from parent theme didn't do much for me.

The use of Template: theme-name inside the theme's style.css has said to be problematic, as well. But without that line of code the child theme becomes a standalone theme.

How can I make my custom page template work in a child theme? Has anyone been through the same situation?

Share Improve this question asked Feb 26, 2019 at 14:49 Victor OliveiraVictor Oliveira 314 bronze badges 1
  • I was actually looking at the problem the wrong way. So, what actually happened is that I was trying to use a page template in my home page, however the parent theme had a front-page.php which lead to my template not being loaded. – Victor Oliveira Commented Mar 21, 2019 at 13:38
Add a comment  | 

2 Answers 2

Reset to default 0

Edit: I see you've answered your own question, but the answer you provided seems dubious to me, so I'm going to post this anyway.

A child theme needs a few key elements to work: a style.css with header information (including Template: parent-theme-name), a functions.php file (where you can enqueue the parent theme's stylesheet), and activation of the child theme from the WordPress admin. Learn more about child themes in the WordPress Codex.

Once you have your child theme activated, all files in the child theme will override templates in the parent theme with the same name. Take a look at the template hierarchy to better understand the names of these templates and how they take precedence.

Finally, let's talk about page templates. You have the right idea by calling the file concert.php since for custom page templates, the file name should not be prepended with page-. This is used for specific slug- or id-driven page templates. At the top of this file, you need to include /* Template Name: Name of Template */, just like you have already. This will appear in the page attributes under Template. After creating this file and storing it in the child theme, you must select this template from the drop-down list under page attributes for your new page. More information on page templates in the WordPress Codex.

I hope this helps!

Never mind. I have deleted front-page.php from the parent theme once again and now it is working. For some odd reason it didn't work the first time. Maybe I had some other thing set up incorrectly as well.

If anyone can close this thread, please do so!

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748863011a314345.html

最新回复(0)