如何从 WooCommerce Storefront 主题主页隐藏页面标题?

How to hide page title from WooCommerce Storefront theme homepage?

我想在我的主页上隐藏店面页面标题。此代码将它从所有侧面隐藏:

function sf_change_homepage_title( $args ) {
    remove_action( 'storefront_page', 'storefront_page_header', 10 );
}
add_action( 'init', 'sf_change_homepage_title' );

但我不能使用 is_front_page(),因为 WordPress 在 $wp_query object 已设置为当前页面之前加载 functions.php,如前所述here.

我不想使用插件 "Title Toggle for Storefront Theme"。

谢谢。

您没有理解链接到的答案。您不能在 functions.php 中使用 is_front_page(),但您完全可以在回调函数中使用它。

The is_front_page() conditional is only available after the query is setup, which happens at init.

所以这个:

function sf_change_homepage_title( $args ) {
    if(is_front_page()) {
        remove_action( 'storefront_page', 'storefront_page_header', 10 );
    }
}
add_action( 'init', 'sf_change_homepage_title' );

会起作用。

解决办法是把"init"换成"wp":

add_action( 'wp', 'sf_change_homepage_title' );

谢谢。