megclaypool
7/19/2018 - 1:42 AM

WP function to show current template

WP function to show current template

  1. Add the following to your theme's functions.php file:
add_filter( 'template_include', 'var_template_include', 1000 );
function var_template_include( $t ){
    $GLOBALS['current_theme_template'] = basename($t);
    return $t;
}

function get_current_template( $echo = false ) {
    if( !isset( $GLOBALS['current_theme_template'] ) )
        return false;
    if( $echo )
        echo $GLOBALS['current_theme_template'];
    else
        return $GLOBALS['current_theme_template'];
}
  1. Call the function in a php file using get_current_template() or in a twig file using {{ function('get_current_template') }}.