Call wp_nav_menu()
before wp_head()
is called, and store the result for later usage:
<head>
<?php
$menu = wp_nav_menu(
array(
'echo' => FALSE,
'theme_location' => 'primary',
'walker' => new Special_Walker
)
);
wp_head();
?>
</head>
<body>
<?php
echo $menu;
Now you can enqueue the CSS from your custom walker.
In your walker collect all CSS rules and add a callback to wp_head
to print them out:
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>';
}
}