你可以使用custom walker 或者只是filter the menu title. 这取决于你需要额外内容的位置:它应该出现在链接之前还是内部?
walker更新示例:实际上,这不起作用:父函数创建<li
, 因此,您必须复制和调整整个父函数
wp_nav_menu(
array (
\'walker\' => new WPSE_45647_Walker
)
);
class WPSE_45647_Walker extends Walker_Nav_Menu
{
public function start_el( &$output, $item, $depth, $args )
{
$output .= $this->custom_content( $item );
parent::start_el( &$output, $item, $depth, $args );
}
/**
* Create your extra content here.
* @return string
*/
protected function custom_content( $item )
{
// inspect the item and return your
// custom content as a string
}
}
过滤器示例更为粗糙,但可能更容易理解:抓住
<li>
和替换
<a
具有
$custom <a
add_filter( \'walker_nav_menu_start_el\', \'wpse_45647_add_custom_content\', 10, 2 );
function wpse_45647_add_custom_content( $item_output, $item )
{
static $counter = 0;
// You may inspect $item and do something more creative here.
$custom = ++$counter . \' Hello World!\';
return str_replace( \'<a \', $custom . \'<a \', $item_output );
}