我正在開發一個WooCommerce插件(實際上是通常的WP插件,但只在啓用WooCommerce時才起作用),它應該改變標準的WooCommerce輸出邏輯。特別是我需要自己重寫標準的archive-product.php模板。 我發現在主題中更改模板沒有問題,但不能在插件中如何執行。我可以怎麼做沒有 WP & WooCommerce核心的任何變化?WooCommerce插件模板覆蓋
0
A
回答
2
我認爲你需要通過WooCommerce的鉤子(過濾器&動作)來實現。
這裏是一個列表: http://docs.woothemes.com/document/hooks/#templatehooks
這是從哪裏開始的掛鉤: http://wp.tutsplus.com/tutorials/the-beginners-guide-to-wordpress-actions-and-filters/
0
這是我嘗試這樣的事情。希望它會有所幫助。
添加此過濾器到你的插件:
add_filter('template_include', 'my_include_template_function');
然後回調函數將
function my_include_template_function($template_path) {
if (is_single() && get_post_type() == 'product') {
// checks if the file exists in the theme first,
// otherwise serve the file from the plugin
if ($theme_file = locate_template(array ('single-product.php'))) {
$template_path = $theme_file;
} else {
$template_path = PLUGIN_TEMPLATE_PATH . 'single-product.php';
}
} elseif (is_product_taxonomy()) {
if (is_tax('product_cat')) {
// checks if the file exists in the theme first,
// otherwise serve the file from the plugin
if ($theme_file = locate_template(array ('taxonomy-product_cat.php'))) {
$template_path = $theme_file;
} else {
$template_path = PLUGIN_TEMPLATE_PATH . 'taxonomy-product_cat.php';
}
} else {
// checks if the file exists in the theme first,
// otherwise serve the file from the plugin
if ($theme_file = locate_template(array ('archive-product.php'))) {
$template_path = $theme_file;
} else {
$template_path = PLUGIN_TEMPLATE_PATH . 'archive-product.php';
}
}
} elseif (is_archive() && get_post_type() == 'product') {
// checks if the file exists in the theme first,
// otherwise serve the file from the plugin
if ($theme_file = locate_template(array ('archive-product.php'))) {
$template_path = $theme_file;
} else {
$template_path = PLUGIN_TEMPLATE_PATH . 'archive-product.php';
}
}
return $template_path;
}
我檢查了這對主題先行加載。如果在主題中找不到該文件,則會從插件中加載該文件。
您可以在此更改邏輯。
希望它會做你的工作。
謝謝
相關問題
- 1. Woocommerce覆蓋插件操作
- 2. joomla模板覆蓋搜索插件
- 3. JOOMLA:插件的模板覆蓋
- 4. WooCommerce:更改按鈕類而不覆蓋模板文件
- 5. 覆蓋FormView控件模板
- 6. Django部件覆蓋模板
- 7. 覆蓋EditorForModel模板
- 8. TYPO3模板覆蓋
- 9. Joomla模板覆蓋
- 10. 從兒童主題覆蓋生成器WooCommerce模板
- 11. ui.bootstrap.tpls覆蓋模板/ datepicker/popup.html模板
- 12. 如何覆蓋@ Html.LabelFor模板?
- 13. 柱模板被覆蓋
- 14. Symfony2 - 覆蓋模板包
- 15. 如何覆蓋notFound模板?
- 16. Mojolicious模板測試覆蓋
- 17. 覆蓋火焰模板
- 18. 覆蓋模板類功能
- 19. 的Joomla K2模板覆蓋
- 20. FosUserBundle覆蓋模板重置
- 21. 覆蓋Angular2/4中的組件模板?
- 22. 覆蓋網頁/模板文件
- 23. 從插件中覆蓋Shopware 4中的模板
- 24. 覆蓋WooCommerce前端Javascript
- 25. 覆蓋WooCommerce CSS問題
- 26. 如何在模板工具包模板文件中覆蓋WRAPPER?
- 27. 如何覆蓋jbot已覆蓋的模板?
- 28. 打印覆蓋模板覆蓋範圍內的路徑
- 29. Eclipse javadoc覆蓋插件
- 30. jQuery插件覆蓋參數