locate_template() 用來檢索存在的優先順序最高的模板檔案,還能直接載入模板檔案。

locate_template() 函式檢索時,如果有子主題則優先使用子主題的模板,沒有再繼續檢索父主題。

用法

1

locate_template($template_names,$load,$require_once);

引數

$template_names

(陣列)(必須) 要引入的模板檔名 (需要副檔名),會根據陣列逐個匹配檔案是否存在,越前邊的優先順序越高。

預設值:None

$load

(布林)(可選) 如果設定成 True 則直接引入模板檔案。

預設值:False

$require_once

(布林)(可選) 如果設定成 True 則如果之前引入過這次不再引入 (require_once),否則無論是否引入過都會引入 (require) 。

(只有 $load 為 True,此引數才生效) 。

預設值:False

返回值

(字串) 只要有一個指定的模板檔案存在則返回它的路徑,否則返回空字串。

例子

if(locate_template('content-'.$pageName.'.php')!==''){

    //存在,引入模板檔案

    get_template_part('content',$pageName);

}else{

    //不存在,直接顯示內容

    the_content();

}

其它

此函式位於:wp-includes/template.php