Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moving Wordpress Theme Template Files to Subdirectory

Tags:

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.

like image 744
Moshe Avatar asked Mar 08 '20 16:03

Moshe


People also ask

How do I move a WordPress template to a page?

In the WordPress editor, you find an option field called 'Page Attributes' with a drop-down menu under 'Template'. Clicking on it will give you a list of available page templates on your WordPress website. Choose the one you desire, save or update your page and you are done.

How do I create a subdirectory in WordPress?

Creating a New SubdirectoryNavigate to your root WordPress directory (often called public_html) and click on the Folder button in the top-left section of the screen. Insert the name of the new folder—this will be the name of your subdirectory—in the popup window and press the Create New Folder button afterward.

Can I import a template into WordPress?

Import custom layout templates and saved rows, columns, and modules​ On the WordPress admin panel, go to Tools > Import, scroll down to WordPress, and click Run Importer. If this is your first time importing, the link in the WordPress section says Install now.

How do I create a template part in WordPress?

To manually create template parts with code, you need an HTML file for each template part. -The file does not include the template part block itself but rather the inner blocks, the content inside the template part. Example of how to create a header: Create a new HTML file called header.


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

like image 148
kaize Avatar answered Sep 30 '22 21:09

kaize