[英]Remove parent theme action in child theme
我已經從Shophistic Lite
主題創建了 Child 主題。
我想刪除子主題中的一個動作。
// wp-content\\plugins\\woocommerce\\templates\\content-product.php ...
/**
* woocommerce_shop_loop_item_title hook.
*
* @hooked woocommerce_template_loop_product_title - 10
*/
do_action( 'woocommerce_shop_loop_item_title' );
/**
* woocommerce_after_shop_loop_item_title hook.
*
* @hooked woocommerce_template_loop_rating - 5
* @hooked woocommerce_template_loop_price - 10
*/
do_action( 'woocommerce_after_shop_loop_item_title' );
/**
* woocommerce_after_shop_loop_item hook.
*
* @hooked woocommerce_template_loop_product_link_close - 5
* @hooked woocommerce_template_loop_add_to_cart - 10
*/
do_action( 'woocommerce_after_shop_loop_item' );
...
// \\wp-content\\themes\\shophistic-lite\\framework\\functions\\woocommerce_support.php
...
/**
* Adds the Switch View buttons
*/
function shophistic_lite_show_attribute() {
...
}
add_action( 'woocommerce_after_shop_loop_item_title', 'shophistic_lite_show_attribute', 15 );
...
// \\wp-content\\themes\\shophistic-lite-child\\functions.php
...
function remove_functions() {
remove_action( 'woocommerce_after_shop_loop_item_title', 'shophistic_lite_show_attribute', 15 );
}
add_action('woocommerce_after_shop_loop_item' , 'remove_functions' );
...
我通過這篇文章做到了: https : //code.tutsplus.com/tutorials/a-guide-to-overriding-parent-theme-functions-in-your-child-theme--cms-22623但它不起作用為了我。 shophistic_lite_show_attribute
函數仍在執行。
請幫我解決這個問題。
您應該嘗試使用'init'
鈎子代替您的刪除操作,以便在初始化時將其刪除:
function child_custom_actions() {
remove_action( 'woocommerce_after_shop_loop_item_title', 'shophistic_lite_show_attribute', 15 );
}
add_action( 'init' , 'child_custom_actions' );
此代碼位於活動子主題(或主題)的 function.php 文件或任何插件文件中。
此代碼經過測試並有效(見下文) 。
與您的評論相關的更新:
我已在測試服務器中上傳了您的主題Shophistic Lite
,並創建了一個子主題。
我臨時更改了woocommerce_support.php
文件中主題的功能,以使其顯示某些內容,無需任何特殊設置(並以這種方式保存):
/**
* Adds the Switch View buttons
*/
function shophistic_lite_show_attribute() {
echo '<p>BLA BLA</p>';
}
add_action( 'woocommerce_after_shop_loop_item_title', 'shophistic_lite_show_attribute', 15 );
它在商店頁面上顯示:
然后我在子主題的function.php
文件中添加了上面第一個片段的代碼(與您嘗試使用 'init' 鈎子的方法相同) ,它運行完美,刪除了“BLA BLA”。
因此,您可能正在嘗試刪除此鈎子未生成的其他內容……
就我而言,我試圖從單個產品頁面中刪除相關產品部分。 它不是默認的相關產品部分,它特定於我的主題(因此我必須從父主題中刪除操作)。
使用wp_loaded
鈎子工作正常,而上面提到的init
鈎子不起作用。
所以最后,我的子主題中的這段代碼運行良好:
function child_custom_actions() {
remove_action( 'woocommerce_after_single_product', 'woocommerce_output_related_products', 20 );
}
add_action( 'wp_loaded' , 'child_custom_actions' );
警告: woocommerce_after_single_product
鈎子不是 woocommerce 相關產品部分的默認鈎子,它特定於我的主題,您可能需要使用woocommerce_after_single_product_summary
鈎子。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.