我只想在用户登录时显示标题栏(位于菜单上)。这是我到目前为止所得到的:
add_action('init','remove_header_cart_if_user_not_logged_in');
function remove_header_cart_if_user_not_logged_in() {
if (is_user_logged_in()) {
return;
} else {
add_action( 'init', 'woa_remove_header_cart' );
function woa_remove_header_cart() {
remove_action( 'storefront_header', 'storefront_header_cart', 60 );
}
}
此代码会产生错误并阻止我的网站显示。 “[域]页面无效[域]目前无法处理此请求。 HTTP ERROR 500“
单独的else部分(woa_remove_header_cart)运行良好,但是当我尝试将其置于“if user logged in”状态时,它会生成错误。
答案 0 :(得分:4)
如果将其简化为:
,该怎么办?add_action( 'storefront_header','remove_header_cart_if_user_not_logged_in' );
function remove_header_cart_if_user_not_logged_in() {
if ( ! is_user_logged_in() ) {
remove_action( 'storefront_header', 'storefront_header_cart', 60 );
}
}
您是在同一时间/优先级向init
挂钩添加两个函数吗?它充其量是奇怪的,可能会导致您的错误。我也不确定WP是否知道用户是由init
登录的。现在没时间检查,但你可以避免它。您不必删除init
挂钩上的函数,只需在执行函数之前执行此操作即可。在我的示例中,我使用的是storefront_header
挂钩,但由于默认(10)优先级低于60
,因此它应该可以正常工作。