'WordPress and a custom folder for page-templates

I want to add my custom page-templates for Wordpress in a custom named folder (eg. not 'page-templates').

Although following code works, for ALL page-templates, not just homepage, this code doesn't make sense to me?

function custom_page_template_directory( $page_template ){
  $BASE = get_stylesheet_directory() .'/template-pages';

  if ( is_page( 'homepage' ) ) {
      $page_template = $BASE . '/page-homepage.php';
  }
  return $page_template;
}

something like this would make more sense, although it's offcourse fictional. Does anyone know how to actually do this?

function custom_page_template_directory( $page_template ){
  $BASE = get_stylesheet_directory() .'/template-pages';

  if ( file_exists( $BASE . post -> post-template ) ) {
      $page_template = $BASE . post -> post-template;
  }
  return $page_template;
}

Note: this is needed for page-templates only, I've already got a nice solution for single-post-templates



Solution 1:[1]

it is not necessary to do this. Wordpress recognizes theme files by name. For example, if you want to make the home template, you just have to create a file called home.php.

if you need to list posts: archive.php and if you have a working post type: archive-posttypeslug.php

In the case of pages it is different, you have the page.php and if you want a custom, you have to generate the file and then assign it from the wordpress backend (in the edition of that page).

Here is some information on this topic, I hope it helps you.

https://codex.wordpress.org/Theme_Development https://developer.wordpress.org/themes/basics/template-hierarchy/

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Maurovez