我使用 Storefront 的子主题。我编辑了 header.php
文件的副本,并在删除导航栏后在此部分中添加了额外的 div:
<header id="masthead" class="site-header" role="banner" style="<?php storefront_header_styles(); ?>">
<div class="col-full">
<?php
/**
* Functions hooked into storefront_header action
*
* @hooked storefront_skip_links - 0
* @hooked storefront_social_icons - 10
* @hooked storefront_site_branding - 20
* @hooked storefront_secondary_navigation - 30
* @hooked storefront_product_search - 40
* @hooked storefront_primary_navigation_wrapper - 42
* @hooked storefront_primary_navigation - 50
* @hooked storefront_header_cart - 60
* @hooked storefront_primary_navigation_wrapper_close - 68
*/
**remove_action( 'storefront_header', 'storefront_primary_navigation', 50 );**
**add_action('storefront_header', 'storefront_primary_navigation', 51);**
**add_action('storefront_header', 'jk_storefront_header_content', 50);**
do_action( 'storefront_header' );
?>
</div>
</header><!-- #masthead -->
这似乎是正确的,但我没有删除和替换导航栏,而是使用旧的导航栏、额外的 div 和另一个导航,这意味着导航栏从未被删除......
如何解决这个问题?
最佳答案
您不需要覆盖子主题中的header.php
...
要正确使用 remove_action()
,您需要将其嵌入到挂接到 init
操作 Hook 中的自定义函数中,如下所示:
add_action('init', 'replace_storefront_primary_navigation' );
function replace_storefront_primary_navigation(){
remove_action( 'storefront_header', 'storefront_primary_navigation', 50 );
add_action('storefront_header', 'jk_storefront_header_content', 50);
}
function jk_storefront_header_content(){
// your custom navigation code goes here
echo '<span style="display:inline-block; padding:10px; border:solid 1px grey;">My custom mega menu goes Here</span>';
}
代码位于事件子主题(或事件主题)的 function.php 文件中。
经过测试并适用于 WooCommerce 店面主题。
关于php - 使用删除和添加操作对 Woocommerce Storefront header 的内容重新排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48141069/