|
| 1 | +<?php |
| 2 | + |
| 3 | +if ( ! function_exists( 'get_partial' ) ) : |
| 4 | + |
| 5 | + /** |
| 6 | + * Load given template with arguments as array. |
| 7 | + * arguments. |
| 8 | + * @see get_template_part(). |
| 9 | + * @see http://wordpress.stackexchange.com/a/103257 |
| 10 | + * @author Julien Vasseur julien@poigneedemainvirile.com |
| 11 | + */ |
| 12 | + function get_partial( $slug = null, $name = null, array $params = array(), $prefix = null ) { |
| 13 | + global $posts, $post, $wp_did_header, $wp_query, $wp_rewrite, $wpdb, $wp_version, $wp, $id, $comment, $user_ID; |
| 14 | + |
| 15 | + /** |
| 16 | + * Fires before the specified template part file is loaded. |
| 17 | + * |
| 18 | + * The dynamic portion of the hook name, `$slug`, refers to the slug name |
| 19 | + * for the generic template part. |
| 20 | + * |
| 21 | + * @param string $slug The slug name for the generic template. |
| 22 | + * @param string $name The name of the specialized template. |
| 23 | + */ |
| 24 | + do_action( "get_partial_{$slug}", $slug, $name ); |
| 25 | + do_action( "get_template_part_{$slug}", $slug, $name ); |
| 26 | + |
| 27 | + $templates = array(); |
| 28 | + $name = (string) $name; |
| 29 | + if ( '' !== $name ) { |
| 30 | + $templates[] = "{$slug}-{$name}.php"; |
| 31 | + } |
| 32 | + |
| 33 | + $templates[] = "{$slug}.php"; |
| 34 | + |
| 35 | + $_template_file = locate_template( $templates, false, false ); |
| 36 | + |
| 37 | + if ( is_array( $wp_query->query_vars ) ) { |
| 38 | + extract( $wp_query->query_vars, EXTR_SKIP ); |
| 39 | + } |
| 40 | + |
| 41 | + if ( isset( $s ) ) { |
| 42 | + $s = esc_attr( $s ); |
| 43 | + } |
| 44 | + |
| 45 | + if ( ! is_null( $prefix ) ) { |
| 46 | + $flags = EXTR_PREFIX_ALL; |
| 47 | + // ensure prefix doesn't end with an underscore, it is automatically added by extract() |
| 48 | + if ( '_' === $prefix[ strlen( $prefix ) - 1 ] ) { |
| 49 | + $prefix = substr( $prefix, 0, -1 ); |
| 50 | + } |
| 51 | + } else { |
| 52 | + $flags = EXTR_PREFIX_SAME; |
| 53 | + $prefix = ''; |
| 54 | + } |
| 55 | + |
| 56 | + extract( $params, $flags, $prefix ); |
| 57 | + |
| 58 | + require( $_template_file ); |
| 59 | + } |
| 60 | + |
| 61 | +endif; |
0 commit comments