Making WordPress.org

Changeset 6652


Ignore:
Timestamp:
02/16/2018 02:22:58 AM (7 years ago)
Author:
obenland
Message:

Main: Add child page templates to page hierarchy.

H/t ocean90.
See #3046.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-main/functions.php

    r6583 r6652  
    8787
    8888/**
     89 * Adds child-page-specific template name to page template hierarchy.
     90 *
     91 * @param array $templates A list of template candidates, in descending order of priority.
     92 * @return array List of filtered template candidates.
     93 */
     94function child_page_templates( $templates ) {
     95    $page = get_queried_object();
     96
     97    if ( $page->post_parent ) {
     98        $parent = get_post( $page->post_parent );
     99
     100        // We want it before page-{page_name}.php but after {Page Template}.php.
     101        $page_name_index = array_search( "page-{$page->post_name}.php", $templates );
     102        $top             = array_slice( $templates, 0, $page_name_index );
     103        $bottom          = array_slice( $templates, $page_name_index );
     104
     105        $templates = array_merge( $top, ["page-{$parent->post_name}-{$page->post_name}.php"], $bottom );
     106    }
     107
     108    return $templates;
     109}
     110add_filter( 'page_template_hierarchy', __NAMESPACE__ . '\child_page_templates' );
     111
     112/**
    89113 * Custom template tags.
    90114 */
Note: See TracChangeset for help on using the changeset viewer.