Moving Wordpress Theme Template Files to Subdirectory

Viewed 457

I would like to restructure the template files in a Wordpress theme that I am creating. Right now, files such as single.php and archive.php are on the root level of my theme. I would like to move them into their own folder -- say a folder called pages. So that it would look something like this:

mytheme 
  -- pages
      -- archive.php
      -- single.php
  -- functions.php
  -- index.php
  -- style.css

Is this possible? If so, how?

Thanks.

1 Answers

You can use the single_template filter and {$type}_template filter for the archive, category etc.

I thing that something like this is what you look for:

function get_new_single_template( $single_template ) {
  global $post;
    $single_template = get_stylesheet_directory() . '/pages/single.php';
  return $single_template;
}
add_filter( 'single_template', 'get_new_single_template' );

function get_new_archive_template( $archive_template ) {
  global $post;
    $archive_template = get_stylesheet_directory() . '/pages/archive.php';
  return $archive_template;
}
add_filter( 'archive_template', 'get_new_archive_template' );

This goes to your functions.php

Related