WordPress的使用的functions.php只在特定的页面模板运行功能(Wordpress

2019-08-02 06:42发布

我有下面的代码检查,如果当前使用的文件是tmp_home_page.php ,但是当我做echo $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

            }
     }

任何想法如何确保我do stuff只主页上运行,并给出模板时,在使用?

Answer 1:

你看上去太辛苦成这样了,还有针对WordPress的功能。 is_page_template()

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

另外, is_front_page()通常是更好的替代is_home()尝试确定用户是否正在观看WordPress网站的前/第一页时。 你可以看到更多的关于这个问题在这里 。



文章来源: Wordpress use functions.php to run function only on specific page template