Skip to content

Instantly share code, notes, and snippets.

@tormjens
Created December 11, 2014 09:58
Show Gist options
  • Save tormjens/4752ded567aebded3443 to your computer and use it in GitHub Desktop.
Save tormjens/4752ded567aebded3443 to your computer and use it in GitHub Desktop.
Improved get_template_part
/**
* An improved version for loading template parts.
*
* Load a template part into a template
*
* Makes it easy for a theme to reuse sections of code in a easy to overload way
* for child themes.
*
* Includes the named template part for a theme or if a name is specified then a
* specialised part will be included. If the theme contains no {slug}.php file
* then no template will be included.
*
* The template is included using require, not require_once, so you may include the
* same template part multiple times.
*
* For the $name parameter, if the file is called "{slug}-special.php" then specify
* "special".
*
* @since 3.0.0
*
* @param string $slug The slug name for the generic template.
* @param string $name The name of the specialised template.
* @param array $parameters Additional parameteres that should be availiable in the template
*/
function new_get_template_part( $slug, $name = null, $parameters = array() ) {
/**
* Fires before the specified template part file is loaded.
*
* The dynamic portion of the hook name, `$slug`, refers to the slug name
* for the generic template part.
*
* @since 3.0.0
*
* @param string $slug The slug name for the generic template.
* @param string $name The name of the specialized template.
*/
do_action( "get_template_part_{$slug}", $slug, $name );
$parameters = apply_filters( "get_template_part_{$slug}_parameters", $parameters, $slug, $name );
extract( $parameters, EXTR_SKIP );
$templates = array();
$name = (string) $name;
if ( '' !== $name )
$templates[] = "{$slug}-{$name}.php";
$templates[] = "{$slug}.php";
include( locate_template($templates, false, false) );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment