WordPress使用functions.php仅在特定页面模板上运行功能

我有以下代码来检查当前使用的文件是否为tmp_home_page.php, 但是当我回显$ template_file时;它显示functions.php

    add_action('template_redirect', 'are_we_home_yet', 10);
    function are_we_home_yet(){
            global $template;
            $template_file = basename((__FILE__).$template);
            if ( is_home() && $template_file == 'tmp_home_page.php' ) {

            // do stuff

            }
     }

任何想法如何确保我的工作只在主页上运行以及何时使用给定的模板?


#1


你对此太过费劲了, 它有一个wordpress函数。 is_page_template()

if( is_page_template( 'tmp_home_page.php' ) && is_home() ){
    // Do Stuff
}

另外, 在尝试确定用户是否正在查看WordPress网站的首页/首页时, is_front_page()通常是is_home()的更好替代方案。你可以在此处查看有关该主题的更多信息。

来源:

https://www.srcmini02.com/64581.html

微信公众号
手机浏览(小程序)
0
分享到:
没有账号? 忘记密码?