我正在开发一个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. Woocommerce模板覆盖不支持woocommerce.php
- 3. Woocommerce模板覆盖不起作用
- 4. WordPress的插件模板覆盖 - 组合
- 5. JOOMLA:插件的模板覆盖
- 6. joomla模板覆盖搜索插件
- 7. Joomla 3模板覆盖插件
- 8. WooCommerce:更改按钮类而不覆盖模板文件
- 9. 覆盖FormView控件模板
- 10. Django部件覆盖模板
- 11. 跻覆盖模板
- 12. TYPO3模板覆盖
- 13. Joomla模板覆盖
- 14. Sylius覆盖模板
- 15. FOSUserBundle - 覆盖模板
- 16. 覆盖EditorForModel模板
- 17. WooCommerce - 覆盖运费
- 18. 从儿童主题覆盖生成器WooCommerce模板
- 19. ui.bootstrap.tpls覆盖模板/ datepicker/popup.html模板
- 20. 覆盖样式中的visualstate而不覆盖控件模板
- 21. django覆盖管理模板
- 22. Symfony2 - 覆盖模板包
- 23. 的Joomla K2模板覆盖
- 24. 如何覆盖notFound模板?
- 25. Symfony:Route覆盖其他模板
- 26. 覆盖Drupal节点模板
- 27. 如何覆盖@ Html.LabelFor模板?
- 28. Rails覆盖迁移模板
- 29. 覆盖模板类功能
- 30. 柱模板被覆盖