Skip to content

Instantly share code, notes, and snippets.

@Phize
Last active December 27, 2015 13:39
Show Gist options
  • Save Phize/7334629 to your computer and use it in GitHub Desktop.
Save Phize/7334629 to your computer and use it in GitHub Desktop.
WordPress: passing arguments to get_template_part() #wp
<?php
function my_get_template_part( $slug, $name = null, $vars = [] ) {
do_action( 'get_template_part_' . $slug, $slug, $name );
$templates = [];
$name = (string) $name;
if ( $name !== '' ) {
$templates[] = $slug . '-' . $name . '.php';
}
$templates[] = $slug . '.php';
call_user_func( function ( $__templates, $__vars ) {
extract( $__vars, EXTR_SKIP );
include( locate_template( $__templates, false, false ) );
}, $templates, $vars );
}
// my_get_template_part( 'slug', 'name', [ 'var' => $var ] );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment