我正在为自己的目的开发一个主题框架。
所以这次我面临一个问题,那就是add_action
计算边条件。
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 );
否则,如果使用这样的条件,它将不起作用
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 );
}
不知道为什么它不起作用。有什么意见吗?
抱歉,代码错误。更新了问题。
SO网友:Jacob Peattie
is_single()
直到wp
钩子,在加载插件和主题后运行。所以,如果你在钩子外面使用它,那么它总是错误的。如果需要基于该条件定义一组挂钩,请在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 );
}
}
);