標準の WooCommerce 出力ロジックを変更する WooCommerce プラグイン (実際には通常の WP プラグインですが、WooCommerce が有効な場合にのみ機能します) を開発しています。特に、標準の archive-product.php テンプレートを自分でオーバーライドする必要があります。テーマでテンプレートを変更しても問題ないことがわかりましたが、プラグインでそれを行う方法がわかりません。WP と WooCommerce のコアを変更せずに、どうすればそれを行うことができますか?
質問する
3147 次
2 に答える
2
WooCommerce で利用できるフック (フィルターとアクション) を介して行う必要があると思います。
リストは次のとおりです: http://docs.woothemes.com/document/hooks/#templatehooks
フックを開始する場所は次のとおりです: http://wp.tutsplus.com/tutorials/the-beginners-guide-to-wordpress-actions-and-filters/
于 2013-04-07T15:24:06.017 に答える
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;
}
テーマの最初の読み込みでこれを確認します。ファイルがテーマに見つからない場合は、プラグインからロードされます。
ここでロジックを変更できます。
それがあなたの仕事をすることを願っています。
ありがとう
于 2016-02-07T10:50:31.660 に答える