我正在开发一个WooCommerce插件(实际上通常是WP插件,但只有在启用WooCommerce时才有效),这应该改变标准的WooCommerce输出逻辑 . 特别是我需要自己覆盖标准的archive-product.php模板 . 我发现在主题中更改模板没有问题,但是不能在插件中怎么做 . 如何在WP&WooCommerce核心没有任何变化的情况下做到这一点?
我认为你需要通过可用于WooCommerce的钩子(过滤器和动作)来实现 .
这是一个清单:http://docs.woothemes.com/document/hooks/#templatehooks
这里是钩子开始的地方:http://wp.tutsplus.com/tutorials/the-beginners-guide-to-wordpress-actions-and-filters/
这是我尝试这样的事情 . 希望它会有所帮助 .
将此过滤器添加到您的插件中:
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; }
我检查这个主题首次加载 . 如果在主题中找不到该文件,则它将从插件加载 .
你可以在这里改变逻辑 .
希望它能完成你的工作 .
谢谢
2 回答
我认为你需要通过可用于WooCommerce的钩子(过滤器和动作)来实现 .
这是一个清单:http://docs.woothemes.com/document/hooks/#templatehooks
这里是钩子开始的地方:http://wp.tutsplus.com/tutorials/the-beginners-guide-to-wordpress-actions-and-filters/
这是我尝试这样的事情 . 希望它会有所帮助 .
将此过滤器添加到您的插件中:
然后回调函数将是
我检查这个主题首次加载 . 如果在主题中找不到该文件,则它将从插件加载 .
你可以在这里改变逻辑 .
希望它能完成你的工作 .
谢谢