WordPress.org

Make WordPress Core

Ticket #13691: get_template_part.diff

File get_template_part.diff, 1.3 KB (added by wjm, 9 years ago)

get_template_part() accepting multiple template names

  • general-template.php

     
    111111 *
    112112 * @uses locate_template()
    113113 * @since 3.0.0
    114  * @uses do_action() Calls 'get_template_part{$slug}' action.
     114 * @uses do_action() Calls 'get_template_part_{$slug}' action.
    115115 *
    116116 * @param string $slug The slug name for the generic template.
    117  * @param string $name The name of the specialised template.
     117 * @param mixed $names The names of the specialised templates (it can be a string, or an array of names to search for in priority order.).
    118118 */
    119 function get_template_part( $slug, $name = null ) {
    120         do_action( "get_template_part_{$slug}", $slug, $name );
     119function get_template_part( $slug, $names = null ) {
     120        do_action( "get_template_part_{$slug}", $slug, $names );
    121121
    122122        $templates = array();
    123         if ( isset($name) )
    124                 $templates[] = "{$slug}-{$name}.php";
    125 
     123        if ( isset($names) ) {
     124                if ( is_array($names) ) {
     125                        foreach ( $names as $name ) {
     126                                if ( !$name )
     127                                        continue;
     128                                $templates[] = "{$slug}-{$name}.php";
     129                        }
     130                } else {
     131                        $name = (string) $names;
     132                        if ( $name )
     133                                $templates[] = "{$slug}-{$name}.php";
     134                }
     135        }
    126136        $templates[] = "{$slug}.php";
    127137
    128138        locate_template($templates, true, false);