0

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

5
  • Good practice for such cases is to create a "templates" folder in your active theme folder; then, in the WP - Admin when editing a page, in the right options column under "page attributes", you will be able to select your stored page template. Note that you should ideally do this via a child theme of your active theme, as such things may get lost with a theme update. Clear? I'll add the links in the comment below.
    – DevelJoe
    Mar 5 at 18:22
  • Check "Creating Custom Page Templates for Global Use" Section here and all you need to know to get started with child themes here
    – DevelJoe
    Mar 5 at 18:22
  • @DevelJoe Thank you. I currently use this solution, but think about it, if I want 10 custom pages I will have 10 options in the drop down menu of templates in page editing. I don't like this. Of course I use a child theme Mar 7 at 22:00
  • Well if that bothers you so much to have that many options, you could make a single page template that you select for all of your 10 pages. Then you create a folder wherever you want where you setup 10 files with the according 10 page contents. And then, in the code of the single template file, you then require / include the corresponding page file according to the current page slug, for example with a switch which considers the slug of the requested page. Clear?
    – DevelJoe
    Mar 8 at 10:23
  • And for the new css and / or js files, simply code in the functions.php file of your child theme to enqueue them in function of the page ID / slug where you wanna use them, and that part's good as well. By ideally placing the according js and css files in your child themes' js and css folders, which you create if they don't exist yet. Clear?
    – DevelJoe
    Mar 8 at 10:27
0

Something like this:

  1. 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.

  2. 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';

}
?>
  1. In your WP admin, you then select the page template "Custom Pages" under Page Attributes for all the concerned pages.

  2. 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.

1

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.