Wordpress custom pages in a folder
-
23-04-2021 - |
Frage
I cant find something similar
I have a portal running WP and I want to create a mikro-site with 4 or more pages, under the same domain and let say I will name this : 'mikro' (I will use a different design for this mikro-site)
So I will have URLs like : mysite.tld/mikro for mikro-site homepage and mysite.tld/mikro/part1, mysite.tld/mikro/part2, mysite.tld/mikro/part3 for the rest
Best practices say to create in my theme folde PHP files named : page-mikro.php, page-part1.php, page-part2.php, page-part3.php (I will also create the pages in WP, give them the corresponding slugs and set the first page as parent and the rest as children)
How could I move these files in a subdirectory of the theme folder (let say 'mikrosite') and WP can also use them?
I will use a different design for this mikro-site and I am going to use many new css, js, files and I will also have up to 10 pages (so 10 PHP files) so keeping all in a subdirectory is quite important.
Thank you
P.S. creating a folder in the root is not an option
Lösung
Something like this:
In your child theme, create a directory to hold your template files as desired; e.g. a "custom_pages" directory, with the according 10 template php files holding the code for these 10 pages inside, like my_page_1.php, my_page_2.php, and so on.
Also in your child theme folder; you setup ONE template file like my_custom_page.php; with the following content, more or less, in this example using your templates in function of the ID of the requested page:
<?php
/* Template Name: Custom Pages */
if ( is_page( 1, 2, 3 ) ) {
require get_stylesheet_directory().'/custom_pages/my_page_1.php';
} elseif ( is_page( 4, 5, 6 ) ) {
require get_stylesheet_directory().'/custom_pages/my_page2.php';
}
?>
In your WP admin, you then select the page template "Custom Pages" under Page Attributes for all the concerned pages.
In your functions.php file of your child theme; to enqueue your page-specific js and css files; you then add something like:
function insert_custom_scripts() {
if ( is_page( 1, 2, 3 ) ) {
wp_enqueue_script( ... )
} elseif ( is_page( 4, 5, 6 ) ) {
wp_enqueue_script( ... )
}
}
add_action( 'wp_enqueue_scripts', 'insert_custom_scripts' );
for js, and similar for the page - specific stylesheets. For a precise description of the enqueuing functions, check this and this.