
描述
在WordPress二开中,我们也许需要对单独的某个页面进行某些逻辑判断,也或者我们需要每个页面一个js和一个css,这时候我们就需要获取当前模板的名称,而下面介绍的这个函数就是为了实现这功能。
开始
一、在functions.php文件中添加代码
function sliver_show_template_name() { global $template; $template = basename($template); $tem = explode('.', $template); return $tem = $tem[0]; } add_action('wp_head', 'sliver_show_template_name');
二、在模板的任意位置使用
sliver_show_template_name()
假设你在首页使用那么这个函数会输出:index,意味着它会输出你模板的真实文件名称,只不过不带 .php。