为什么这在我的 Genesis 中不起作用!! remove_action 带有条件标签

Why isn't this working in my Genesis!! remove_action with conditional tags

您好,我构建了一个导航小部件,它看起来很棒,可以显示来自 Buddypress 的登录人员,但显然我希望它隐藏在主导航、登录页面等中。

我在这里扯我的头发是因为就所有目的而言我所做的是正确的? - 但它根本不起作用,非常感谢这里的一些帮助!谢谢

/** Register Utility Bar Widget Areas. */
genesis_register_sidebar( array(
 'id' => 'utility-bar-left',
 'name' => __( 'TRN Member Utility Bar left', 'theme-prefix' ),
 'description' => __( 'This is the left utility bar above the header.', 'theme-prefix' ),
) );
genesis_register_sidebar( array(
 'id' => 'utility-bar-right',
 'name' => __( 'TRN Member Utility Bar Right', 'theme-prefix' ),
 'description' => __( 'This is the right utility bar above the header.', 'theme-prefix' ),
) );


add_action( 'genesis_before_header', 'utility_bar' );

function utility_bar() {

 echo '<div class="utility-bar">';

 genesis_widget_area( 'utility-bar-left', array(
 'before' => '<div class="utility-bar-left">',
 'after' => '</div>',
 ) );

 genesis_widget_area( 'utility-bar-right', array(
 'before' => '<div class="utility-bar-right">',
 'after' => '</div>',
 ) );

 echo '</div></div>';
}

add_action('genesis_before_header','remove_bar');

function remove_bar() {
if (is_home() || is_page(www.trnworld.com) || is_page(trn-login) || is_front_page() ) { //Replace post_type with your post type slug
remove_action( 'genesis_before_header', 'utility_bar', 5 );
remove_action( 'genesis_before_header', 'genesis_register_sidebar' );
}
}

来自 Wordpress 文档:

Important: To remove a hook, the $function_to_remove and $priority arguments must match when the hook was added. This goes for both filters and actions. No warning will be given on removal failure.

add_action( 'genesis_before_header', 'utility_bar' );

不匹配

remove_action( 'genesis_before_header', 'utility_bar', 5 );

此外,您已将用于显示小部件的操作和用于删除显示小部件的操作添加到同一个挂钩中。

来自 add_action

上的 Wordpress 文档

$priority (int) (Optional) Used to specify the order in which the functions associated with a particular action are executed. Lower numbers correspond with earlier execution, and functions with the same priority are executed in the order in which they were added to the action.

Default value: 10

您已经添加了两个具有相同默认优先级 10 的操作,并且由于 add_action( 'genesis_before_header', 'utility_bar' );add_action('genesis_before_header','remove_bar'); 之前添加,因此在调用 do_action 函数时它是 运行 第一个在 genesis_before_header 打印出小部件;之后第二个函数运行,对已经执行的操作执行 remove_action。

看看如果你改变会发生什么

add_action('genesis_before_header','remove_bar');add_action( 'genesis_before_header','remove_bar', 9 );

remove_action( 'genesis_before_header', 'utility_bar', 5 );remove_action( 'genesis_before_header', 'utility_bar' );

函数中的不相关注释 utility_bar() 我看到 2 个关闭的 div,但只有一个打开的。我猜这会导致其中一种显示状态出现布局问题。