查找后this answer, 我选择了:
$template_tree = mytheme_get_template_tree();
$total = count($template_tree); //count the elements in the array
end($template_tree); //set the pointer to the end of the array
$immediate_parent = basename(prev($template_tree)); //get the filename of the penultimate element
然后在插件/函数中。php
function mytheme_get_template_tree()
{
$included_files = get_included_files();
$stylesheet_dir = str_replace( \'\\\\\', \'/\', get_stylesheet_directory() );
$template_dir = str_replace( \'\\\\\', \'/\', get_template_directory() );
foreach ( $included_files as $key => $path ) {
$path = str_replace( \'\\\\\', \'/\', $path );
if ( FALSE === strpos( $path, $stylesheet_dir ) && FALSE === strpos( $path, $template_dir ) ) {
unset( $included_files[ $key ] );
}
}
return $included_files;
}
尽管如此,我仍然有兴趣了解其他方法!