問題描述

我想在主題資料夾中檢查一個適當的模板,然後再回到我的 plugin 目錄下的檔案。這是我的程式碼:

add_filter('template_include', 'sermon_template_include');
function sermon_template_include($template) {
    if(get_query_var('post_type') == 'wpfc_sermon') {
        if ( is_archive() || is_search() ) :
           if(file_exists(TEMPLATEDIR . '/archive-wpfc_sermon.php'))
              return TEMPLATEDIR . '/archive-wpfc_sermon.php';
           return dirname(__FILE__) . '/views/archive-wpfc_sermon.php';
        else :
           if(file_exists(TEMPLATEDIR . '/single-wpfc_sermon.php'))
              return TEMPLATEDIR . '/single-wpfc_sermon.php';
           return dirname(__FILE__) . '/views/single-wpfc_sermon.php';
        endif;
    }
    return $template;
}

問題是,它不工作! :-) 它總是在我的外掛資料夾中選擇檔案。任何想法該怎麼辦?我嘗試了很多變化,但我似乎無法做任何工作!提前致謝!插口

編輯

我期待從主題資料夾 (如果存在) 返回 archive-wpfc_sermon.php 。但是,我的外掛中的檔案總是被返回。謝謝你的幫助!這是從我的 Sermon Manager 外掛在儲存庫中可用。

最佳解決方案

所以,我不知道究竟是什麼導致的問題,但你可以嘗試以下:

  1. 外掛檔案路徑:用 plugin_dir_path( __FILE__ )替換 dirname(__FILE__)

  2. 主題檔案路徑:用 get_stylesheet_directory()替換 TEMPLATEDIR

這可能是直接引用常量的問題。

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。