admin管理员组

文章数量:1379790

I am developing a theme framework for my own purposes.

So this time i am facing a problem, that is add_action works out side conditional.

add_action( 'meanz_before_main_wrap', 'meanz_entry_header_structure_open', 5 );
add_action( 'meanz_before_main_wrap', function(){echo "<div class='title_cat'>";}, 6 );
add_action( 'meanz_before_main_wrap', function(){echo meanz_post_cats();}, 7 );
add_action( 'meanz_before_main_wrap', 'meanz_do_title', 25 );
add_action( 'meanz_before_main_wrap', function(){echo "</div>";}, 49 );
add_action( 'meanz_before_main_wrap', 'meanz_do_thumbnail', 50 );
add_action( 'meanz_before_main_wrap', 'meanz_entry_header_structure_close', 100 );

Otherwise if is use a conditional like this, it doesn't work

if(is_single()){
add_action( 'meanz_before_main_wrap', 'meanz_entry_header_structure_open', 5 );
add_action( 'meanz_before_main_wrap', function(){echo "<div class='title_cat'>";}, 6 );
add_action( 'meanz_before_main_wrap', function(){echo meanz_post_cats();}, 7 );
add_action( 'meanz_before_main_wrap', 'meanz_do_title', 25 );
add_action( 'meanz_before_main_wrap', function(){echo "</div>";}, 49 );
add_action( 'meanz_before_main_wrap', 'meanz_do_thumbnail', 50 );
add_action( 'meanz_before_main_wrap', 'meanz_entry_header_structure_close', 100 );
}

Don't know why it doesn't work. Any comments?

Sorry for wrong code. Updated the question.

I am developing a theme framework for my own purposes.

So this time i am facing a problem, that is add_action works out side conditional.

add_action( 'meanz_before_main_wrap', 'meanz_entry_header_structure_open', 5 );
add_action( 'meanz_before_main_wrap', function(){echo "<div class='title_cat'>";}, 6 );
add_action( 'meanz_before_main_wrap', function(){echo meanz_post_cats();}, 7 );
add_action( 'meanz_before_main_wrap', 'meanz_do_title', 25 );
add_action( 'meanz_before_main_wrap', function(){echo "</div>";}, 49 );
add_action( 'meanz_before_main_wrap', 'meanz_do_thumbnail', 50 );
add_action( 'meanz_before_main_wrap', 'meanz_entry_header_structure_close', 100 );

Otherwise if is use a conditional like this, it doesn't work

if(is_single()){
add_action( 'meanz_before_main_wrap', 'meanz_entry_header_structure_open', 5 );
add_action( 'meanz_before_main_wrap', function(){echo "<div class='title_cat'>";}, 6 );
add_action( 'meanz_before_main_wrap', function(){echo meanz_post_cats();}, 7 );
add_action( 'meanz_before_main_wrap', 'meanz_do_title', 25 );
add_action( 'meanz_before_main_wrap', function(){echo "</div>";}, 49 );
add_action( 'meanz_before_main_wrap', 'meanz_do_thumbnail', 50 );
add_action( 'meanz_before_main_wrap', 'meanz_entry_header_structure_close', 100 );
}

Don't know why it doesn't work. Any comments?

Sorry for wrong code. Updated the question.

Share Improve this question asked May 17, 2020 at 4:34 Raashid DinRaashid Din 2182 silver badges19 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

is_single() is not determined until the wp hook, which runs after plugins and themes are loaded. So if you use it outside a hook, then it will always be false. If you need to define a bunch of hooks based on that condition, do it inside a callback for wp:

add_action(
    'wp',
    function() {
        if ( is_single() ) {
            add_action( 'meanz_before_main_wrap', 'meanz_entry_header_structure_open', 5 );
            add_action( 'meanz_before_main_wrap', function(){echo "<div class='title_cat'>";}, 6 );
            add_action( 'meanz_before_main_wrap', function(){echo meanz_post_cats();}, 7 );
            add_action( 'meanz_before_main_wrap', 'meanz_do_title', 25 );
            add_action( 'meanz_before_main_wrap', function(){echo "</div>";}, 49 );
            add_action( 'meanz_before_main_wrap', 'meanz_do_thumbnail', 50 );
            add_action( 'meanz_before_main_wrap', 'meanz_entry_header_structure_close', 100 );
        }
    }
);

本文标签: theme developmentaddaction works outside condition but not inside it