呼叫wp_nav_menu()
之前wp_head()
调用,并存储结果以供以后使用:
<head>
<?php
$menu = wp_nav_menu(
array(
\'echo\' => FALSE,
\'theme_location\' => \'primary\',
\'walker\' => new Special_Walker
)
);
wp_head();
?>
</head>
<body>
<?php
echo $menu;
现在,您可以从自定义walker中将CSS排队。
在walker中,收集所有CSS规则,并将回调添加到wp_head
要将其打印出来:
class Special_Walker extends Walker_Nav_Menu
{
protected $custom_css = array();
function start_el(&$output, $item, $depth, $args) {
add_action( \'wp_head\', array( $this, \'head_css\' ) );
//menu generation code
$this->custom_css[] = \'some css\';//store css code in array
$output .= apply_filters( \'walker_nav_menu_start_el\', $item_output, $item, $depth, $args );
}
public function head_css()
{
print \'<style>\' . join( "\\n", $this->custom_css ) . \'</style>\';
}
}