1

I have number of legal documents of different formats / layout that I need to display on a Wordpress site, however I don't want to have them as individual page-pagename.php files in the root of the project folder. I know I can use get_template_part() and load in different template files, but this won't really help me because the files are all of differing styles.

Is there any way of storing pages in a subfolder called, say, legal, and then pulling in files from this folder as fully complete pages when people click on the appropriate links on the site, otherwise the root folder for the site is just going to be cluttered mess with endless individual page-pagename.php files.

Many thanks

2 Answers 2

1

You can certainly use Page Templates for that. They're a specific type of template file that can be applied to a specific page or groups of pages. Plus, they can be stored in sub folders.

All you need is to put something like this at the top of the template files:

/**
 * Template Name: Legal Page
 */

After that, you can choose the "Legal Page" template when editing a page in the admin.

1

I know this is an old question that already has an answer but it wasn't the solution I was looking for. Adding another option in case anyone else is searching for a way to move all WordPress template files to a subfolder. Found this code for changing the default folder for WordPress template files on Reddit and made a few small adjustments. This code can be placed in the functions.php of your theme or child theme.

/**
 * Tell WordPress that we have moved default template files to the page-templates folder.
 * Based on code from: https://www.reddit.com/r/Wordpress/comments/ffhjvw/moving_wordpress_theme_template_files_to/
 *
 * Related posts with other solutions:
 * https://stackoverflow.com/questions/60589503/moving-wordpress-theme-template-files-to-subdirectory
 * https://wordpress.stackexchange.com/questions/17385/custom-post-type-templates-from-plugin-folder
 * https://wordpress.stackexchange.com/questions/291725/store-page-template-files-in-a-subfolder
 * https://wordpress.stackexchange.com/questions/312159/how-to-move-page-template-files-like-page-slug-php-to-a-sub-directory/312611#312611
 * 
 * Template hierarchy info: 
 * https://developer.wordpress.org/reference/hooks/type_template_hierarchy/
 * https://core.trac.wordpress.org/browser/tags/5.8.1/src/wp-includes/template.php
 * https://developer.wordpress.org/themes/basics/organizing-theme-files/#page-templates-folder
 *
 * @param array $templates A list of candidates template files.
 * @return string Full path to template file.
 */
function change_template_path($templates) {

  // The custom sub-directory for page templates in your theme. 
  $custom_sub_dir = 'page-templates';

  // Don't use the custom template directory in unexpected cases.
  if(empty($templates) || ! is_array($templates)) {
    return $templates;
  }

  $page_template_id = 0;
  $count = count( $templates);
  if($templates[0] === get_page_template_slug()) {
    // if there is a custom template, then our page-{slug}.php template is at the next index
    $page_template_id = 1;
  }

  // The last one in $templates is page.php, single.php, or archives.php depending on the type of template hierarchy being read.
  // Paths of all items starting from $page_template_id will get updated
  for($i = $page_template_id; $i < $count ; $i++) {
    $templates[$i] = $custom_sub_dir . '/' . $templates[$i];
  }

  return $templates;
}

// Add filters to override the path for each WP template hierarchy.
// These are all the template hierarchy filters. You should probably only override the ones you need.
add_filter('404_template_hierarchy', 'change_template_path');
add_filter('archive_template_hierarchy', 'change_template_path');
add_filter('attachment_template_hierarchy', 'change_template_path');
add_filter('author_template_hierarchy', 'change_template_path');
add_filter('category_template_hierarchy', 'change_template_path');
add_filter('date_template_hierarchy', 'change_template_path');
add_filter('embed_template_hierarchy', 'change_template_path');
add_filter('frontpage_template_hierarchy', 'change_template_path');
add_filter('home_template_hierarchy', 'change_template_path');
// If you override the index hierarchy, be sure to add an index.php template in your custom template folder.
add_filter('index_template_hierarchy', 'change_template_path'); 
add_filter('page_template_hierarchy', 'change_template_path');
add_filter('paged_template_hierarchy', 'change_template_path');
add_filter('privacypolicy_template_hierarchy', 'change_template_path');
add_filter('search_template_hierarchy', 'change_template_path');
add_filter('single_template_hierarchy', 'change_template_path');
add_filter('singular_template_hierarchy', 'change_template_path');
add_filter('tag_template_hierarchy', 'change_template_path');
add_filter('taxonomy_template_hierarchy', 'change_template_path');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.