WooCommerce从我的插件文件夹加载并自定义archive-product.php
我正在尝试从WooCommerce从我的插件文件夹加载并自定义archive-product.php,php,woocommerce,wordpress,Php,Woocommerce,Wordpress,我正在尝试从myplugin文件夹加载模板文件,而不是使用默认模板。我想从my plugin文件夹模板结构中定制商店页面库,因此我编辑archive product.php以评论和检查哪个页面显示空白,但它加载默认的woocommerce模板(archive product.php)。我已经将模板文件移动到我的plugin/woocommerce/文件夹,这是我尝试的代码 function cc_woocommerce_locate_template($template, $template_
myplugin
文件夹加载模板文件,而不是使用默认模板。我想从my plugin
文件夹模板结构中定制商店页面库,因此我编辑archive product.php
以评论和检查哪个页面显示空白,但它加载默认的woocommerce模板(archive product.php)
。我已经将模板文件移动到我的plugin/woocommerce/
文件夹,这是我尝试的代码
function cc_woocommerce_locate_template($template, $template_name, $template_path) {
global $woocommerce;
global $post;
$plugin_path = untrailingslashit(plugin_dir_path(__FILE__)) . '/woocommerce/';
//returns my-plugin folder with base file name echo $plugin_path;
// echo $template_name;
if (file_exists($plugin_path . $template_name)) {
$template = $plugin_path . $template_name;
// var_dump($template);
return $template;
}
return $template;
}
我指的钩子是
add_filter('woocommerce_locate_template', 'cc_woocommerce_locate_template', 1, 3);
我测试了提供的代码,它运行时没有问题,因此在这种情况下,最好重新开始。创建一个新插件并粘贴下面的代码。它包含两个过滤器: wc\u get\u template\u part-用于循环中使用的模板 woocommerce\u定位\u模板-用于所有其他模板
现在在plugins主文件夹中创建一个子文件夹
woocommerce
,并复制原始文件archive product.php
和content product.php
。最后,对这些文件进行一些小的修改(回显),看看它们是否已加载。我测试了此示例,在以下层次结构中加载模板时没有问题:
主题/模板\路径/模板\名称
主题/模板名称
插件/模板\路径/模板\名称
默认/模板名称
我找到了一个从插件加载archive-product.php或single-product.php的解决方案。我认为应该行得通
public function include_template_function( $template_path ) {
if ( get_post_type() == 'product' ) {
if ( is_single() ) {
// 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_dir_path( __FILE__ ) . 'woocommerce/single-product.php';
}
}
if ( is_archive() ) {
// 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_dir_path( __FILE__ ) . 'woocommerce/archive-product.php';
}
}
}
_log($template_path);
return $template_path;
}
add_filter( 'template_include', array($this, 'include_template_function' ), 100 );
OMG它工作得很好:)但编辑的更改只会在content-product.php上触发,而不会在存档产品中触发。php奇怪的是,如果启动了该函数,那么它就不是兼容性问题(我使用的是WC 2.1.5),也许您安装了另一个插件,覆盖了您的插件。将woocommerce\u locate\u模板过滤器的优先级从10提高到100,然后再次测试。非常感谢,它帮助了很多+1:)
public function include_template_function( $template_path ) {
if ( get_post_type() == 'product' ) {
if ( is_single() ) {
// 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_dir_path( __FILE__ ) . 'woocommerce/single-product.php';
}
}
if ( is_archive() ) {
// 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_dir_path( __FILE__ ) . 'woocommerce/archive-product.php';
}
}
}
_log($template_path);
return $template_path;
}
add_filter( 'template_include', array($this, 'include_template_function' ), 100 );