替换整个商店中的<div id= "content" >



我正在寻求移除<div id="content"><div class="woocommerce">包装容器,并将其替换为与我的网站其他部分匹配的更合适的标签。

我第一次尝试行动:

remove_action( 'woocommerce_before_main_content', 'woocommerce_output_content_wrapper', 20); 
remove_action( 'woocommerce_after_main_content', 'woocommerce_output_content_wrapper_end', 20); 
function WOO_before_main_content( $woocommerce_output_content_wrapper ) { 
echo '<main class="woocommerce">';
}
function WOO_after_main_content( $woocommerce_output_content_wrapper ) { 
echo '</main>';
}
// Use <main> instead, much better.
add_action('woocommerce_after_main_content', 'WOO_after_main_content', 5);
add_action( 'woocommerce_before_main_content',  'WOO_before_main_content', 5); 

这似乎适用于某些页面(如类别或产品页面),但其他页面(如帐户/登录和购物车)则保持不变。

然后我尝试将模板文件复制到我的主题中,并在那里更新HTML。

woocommerce/global/wrapper-start.php
woocommerce/global/wrapper-end.php

同样的结果。知道为什么我不能在整个商店中全局控制包装器吗?

更新

在进一步的研究中,钩子指南将woocommerce_before_main_content动作列为仅由archive-product.php, single-product.php调用。所以我想不是每一页?这让我有点困惑,因为每一篇在线帖子都说要用它来编辑包装HTML元素。也许最近发生了什么变化?

搜索整个Wooccommerce插件目录,global/wrapper-start.php是唯一包含id="content"的东西,所以它一定来自那里。同样,试图将此模板覆盖到主题的woocomerce/global/wrapper-start.php中是行不通的。

因此,追溯到更远的地方,wc-template-functions.php中的woocommerce_output_content_wrapper()函数是唯一加载包装器启动模板的东西。我在我的functions.php文件中尝试在Wooccommerce之前定义它,但它也不起作用。

我一定是错过了什么。

您需要删除一个优先级与添加优先级相同的操作。在您的代码中,您使用20作为优先级,但WooCommerce将其添加为10。

// First remove default wrapper
remove_action( 'woocommerce_before_main_content', 'woocommerce_output_content_wrapper', 10);
remove_action( 'woocommerce_after_main_content', 'woocommerce_output_content_wrapper_end', 10);
// Then add new wrappers
add_action('woocommerce_before_main_content', 'my_theme_wrapper_start', 10);
add_action('woocommerce_after_main_content', 'my_theme_wrapper_end', 10);
function my_theme_wrapper_start() {
echo '<main class="woocommerce">';
}
function my_theme_wrapper_end() {
echo '</main>';
}

最新更新