WordPress.org

Make WordPress Core

Opened 15 months ago

Last modified 2 months ago

#23142 new feature request

Make it possible to "stem" an arbitrary prefix with the Template Hierarchy's "stemming" logic.

Reported by: rulatir Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 3.5
Component: Themes Keywords:
Focuses: template Cc:

Description

Please expose the template hierarchy "stemming" logic for theme developers. By "stemming" I mean the process by which the list of candidate template filenames (e.g. page-meaningoflife.php, page-42.php, page.php) is generated.

I know that the way template-loader.php is currently written, such list is never explicitly assembled. I consider this a deficiency. The "stemming" logic should be available as an API:

function wp_template_candidates($prefix="") {

    $filenames = array();

    // Extensively refactored code from template-loader.php and template.php

    return $filenames;
}

The new template-loader.php could then probably be just reduced almost to this:

locate_template(wp_template_candidates(),true);

But the real motivation is to be able to use a single layout template that includes different versions of "parts" like menu or sidebars depending on the query type and queried object, exactly as the template hierarchy does. This can of course be done with ad-hoc checks:

if (is_home()) { locate_template("home/sidebar.php", true); }

but making this consistent with how template hierarchy works would involve basically reimplementing much of it. The proposed API would make it possible to override parts of a template for specific query types or queried objects just by creating an appropriately located and named file, and without modifying the main layout file:

/*
 *   File: MyTheme/includes/body-layout.php
 */

<div id="main-menu">
<?php locate_template(wp_template_candidates("main-menu/"),true); ?>
</div>

<?php

//On a "video" category page, this will pick MyTheme/social_slider/category-video.php

@include_once locate_template(wp_template_candidates("social_slider/"));

?>

Change History (5)

comment:1 follow-ups: helen15 months ago

  • Keywords close added

Not completely sure I'm reading this right, but sounds like you're asking for exactly what get_template_part() does.

comment:2 in reply to: ↑ 1 alexvorn215 months ago

Replying to helen:

Not completely sure I'm reading this right, but sounds like you're asking for exactly what get_template_part() does.

With get_template_part we can not pass a custom variable, maybe this what author is asking..

<?php get_template_part_new( 'loop', 'index', $excerpt = true ); ?>

comment:3 in reply to: ↑ 1 rulatir15 months ago

  • Keywords close removed

Replying to helen:

Not completely sure I'm reading this right, but sounds like you're asking for exactly what get_template_part() does.

No, in your example you hardcode the 'index' part. I want to be able to instead execute the template hierarchy logic to determine that part exactly the way template_loader.php determines it when loading the main template file.

In other words, at the point in a generic layout template where I expect to place this call:

<?php locate_template(wp_template_candidates('loops/'), true); ?>

... at this point I don't know (and in fact want to abstract away) whether we are showing the homepage, a single post, a category archive, or a category archive for category "foo". If these are four special cases for which I want to use different loop template parts, then I want to be able to create these template parts in the loops/ subdirectory, and get them automatically picked up in appropriate cases.

In yet other words, I want to be able to defer the whole template hierarchy logic to the moment(s) when I load specific template parts. Unfortunately get_template_part cannot do that. It only does child theme to parent theme fallback.

comment:4 SergeyBiryukov15 months ago

  • Component changed from Themes to Template

comment:5 nacin2 months ago

  • Component changed from Template to Themes
  • Focuses template added
Note: See TracTickets for help on using tickets.