Make WordPress Core

Opened 11 years ago

Closed 9 years ago

Last modified 9 years ago

#23142 closed feature request (maybelater)

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

Reported by: rulatir's profile rulatir Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.5
Component: Themes Keywords: needs-patch
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 (7)

#1 follow-ups: @helen
11 years 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.

#2 in reply to: ↑ 1 @alexvorn2
11 years 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 ); ?>

#3 in reply to: ↑ 1 @rulatir
11 years 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.

#4 @SergeyBiryukov
11 years ago

  • Component changed from Themes to Template

#5 @nacin
10 years ago

  • Component changed from Template to Themes
  • Focuses template added

#6 @chriscct7
9 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to maybelater
  • Status changed from new to closed

Closing as maybelater. Complete lack of interest on the feature on the ticket over the last 3 years. Feel free to reopen when more interest re-emerges (particularly if there's a patch).

#7 @chriscct7
9 years ago

  • Keywords needs-patch added
Note: See TracTickets for help on using tickets.