Referencing a subdirectory in a custom page template? (6 posts)

  1. onethousandseas
    Posted 6 years ago #

    If I want to make extra custom templates for my theme, is it possible to put it in a subdirectory within the theme directory to reference it?

    So if the code at the top of a custom page were this:

    Template Name: Snarfer

    how would I reference that if I were to put snarfer.php inside a directory named "extra-templates"?

  2. s_ha_dum
    Posted 6 years ago #

    You can easily put .php files in subdirectories and call them using get_bloginfo('template_url').'/subdirectory/somefile.php'. I don't know if WP will recognize a template file if you put it in a subdirectory. Have you tried that?

  3. onethousandseas
    Posted 6 years ago #

    Yep, I put it inside a subdir, but it didn't work when I simply tried to call it through the control panel.

  4. kvcrawford
    Posted 6 years ago #

    I wish it were possible to put custom page templates in a subdirectory, and still choose them in the "Template" <select> box from "Edit Pages" in the admin panel. It would keep the template's folder a lot cleaner when using a lot of custom page templates.

    It would also be nice if you could keep your stylesheet(s) in their own "./css/" directory.

  5. metal450
    Posted 6 years ago #

    Re: kvcrawford - I couldn't agree more...my theme directory is a mess, and I've resorted to just trying to name everything to keep them together (i.e. pg_xx.php so all my custom page templates get alphabetized next to each other, etc)

  6. metal450
    Posted 6 years ago #

    A simple solution this is to use the template_redirect hook, and call a function in which you to load your templates from wherever you want, i.e.

    add_action('template_redirect', 'template_overrides');
    function template_overrides()
        if( is_page('potd') ):
           include(HOMEPATH . '/potd_/potd.php');
        //fallthrough to default


Topic Closed

This topic has been closed to new replies.

About this Topic