Skip to content

Instantly share code, notes, and snippets.

@iamcanadian1973
Last active October 19, 2017 16:56
Show Gist options
  • Save iamcanadian1973/891581855de856ea110059d11ab863c5 to your computer and use it in GitHub Desktop.
Save iamcanadian1973/891581855de856ea110059d11ab863c5 to your computer and use it in GitHub Desktop.
<?php
/**
* 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".
*
*
* @param string $slug The slug name for the generic template.
* @param null|string $name
* @param array $data optional array of vars to inject into the template part
* @param boolean $return Whether to return or output the template
*/
function _s_get_template_part( $slug, $name = null, $data = array(), $return = false ) {
do_action( "get_template_part_{$slug}", $slug, $name );
$name = (string) $name;
if ( '' !== $name )
$templates[] = "{$slug}-{$name}.php";
$templates[] = "{$slug}.php";
if( is_array( $data ) ) {
extract( $data );
}
// Return instead of echo
if( $return ) {
ob_start();
include( locate_template( $templates ) );
$content = ob_get_contents();
ob_end_clean();
return $content;
}
include( locate_template( $templates ) );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment