I tested this tutorial. It works great.
The problem is that in the tutorial there is one menu while I have 2 menus. Let me know how I can customize this function for the feature only applies to primary menu.
The code :
function new_nav_menu_items($items) {
$items = "";
$args = array(
"post_type" => "page",
"order" => "ASC",
"orderby" => "menu_order"
);
$the_query = new WP_Query($args);
if($the_query->have_posts()):
while($the_query->have_posts()):
$the_query->the_post();
$items .= '<li><a href="#post-'. get_the_ID() .'">' . get_the_title() . '</a></li>';
endwhile;
else:
echo "";
endif;
return $items;
}
add_filter("wp_nav_menu_items", "new_nav_menu_items");
I tried with if( $args->theme_location == 'primary' )
but I do not know how to use it ..
I can not apply this one
I tested this tutorial. It works great.
The problem is that in the tutorial there is one menu while I have 2 menus. Let me know how I can customize this function for the feature only applies to primary menu.
The code :
function new_nav_menu_items($items) {
$items = "";
$args = array(
"post_type" => "page",
"order" => "ASC",
"orderby" => "menu_order"
);
$the_query = new WP_Query($args);
if($the_query->have_posts()):
while($the_query->have_posts()):
$the_query->the_post();
$items .= '<li><a href="#post-'. get_the_ID() .'">' . get_the_title() . '</a></li>';
endwhile;
else:
echo "";
endif;
return $items;
}
add_filter("wp_nav_menu_items", "new_nav_menu_items");
I tried with if( $args->theme_location == 'primary' )
but I do not know how to use it ..
I can not apply this one
1 Answer
Reset to default 1I came across your question by looking for an answer myself. Here is how I made the code work for me:
function new_nav_menu_items( $items, $args ) {
if ( $args->menu == 'primary' ) {
$items = "";
$args = array(
"post_type" => "page",
"order" => "ASC",
"orderby" => "menu_order"
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ):
while( $the_query->have_posts() ):
$the_query->the_post();
$items .= '<li><a href="#post-' . get_the_ID() . '">' .
get_the_title() . '</a></li>';
endwhile;
else:
echo "";
endif;
}
return $items;
}
add_filter( "wp_nav_menu_items", "new_nav_menu_items" );