The Layout and Structure of the WordPress Twenty Twelve Theme
How to Install a WordPress Theme
How to Activate a New WordPress Theme

How to Remove WordPress Template Files

You may be wondering why you’d want to remove a parent’s template file. Unfortunately, the WordPress Twenty Thirteen theme doesn’t provide a good example of why you’d want to do this. Therefore, for future reference, you must use your imagination a bit here to understand the mechanics of removing a file from the parent theme.

Imagine that you’re creating a child theme built off an Example Parent parent theme. Example Parent is well designed, and the child theme looks and works exactly the way you want it to, but you have a problem.

The Example Parent theme has a home.php template file that provides a highly customized page template for use as the home page of your site. This works very well, but it isn’t what you want for the site if you want a standard blog home page. If the home.php file didn’t exist in Example Parent, everything would work perfectly.

You can’t remove the home.php file from Example Parent without modifying the parent theme (which you never, ever want to do), so you have to use a trick. Instead of removing the file, override the home.php file and have it emulate index.php.

You may think that simply copying and pasting the Example Parent index.php code into the child theme’s home.php file would be a good approach. Although this would work, here is a better way: You can tell WordPress to run the index.php file so that changes to index.php are respected. This single line of code inside the child theme’s home.php file is all you need to replace home.php with index.php:

<?php locate_template( array( 'index.php' ), true ); ?>

The locate_template function does a bit of magic. If the child theme supplies an index.php file, it is used. If not, the parent index.php file is used.

This produces the same result that removing the parent theme’s home.php file would have. The home.php code is ignored, and the changes to index.php are respected.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Installing and Configuring the WordPress.com Stats Plugin
Edit a WordPress Theme to Include Photo Gallery Styles
How to Upload Header Images in Twenty Thirteen WordPress Themes
How to Preview and Activate WordPress.com Themes
How to Find and Install Free WordPress Theme
Advertisement

Inside Dummies.com