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.
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With