I agree with ialocin, if you can use the built-in nav menu css classes you should but if those aren’t enough there’s a nice little filter which can help with css classes. If you need something other than CSS classes you can use a Custom Walker ( as ialocin suggested ) and use the same logic as the function below.
Add some CSS Classes to WP Nav Menu
/** Custom `wp_nav_menu()` Classes **/
function add_class_to_wp_nav_menu( $classes, $item ) {
if( hasChildren( $item->object_id ) ) { // hasChildren is a Custom Function
array_push( $classes, 'has-children' );
}
if( 0 !== wp_get_post_parent_id( $item->object_id ) ) { // 0 means there is no parent
array_push( $classes, 'has-parent' );
}
return $classes;
}
add_filter( 'nav_menu_css_class', 'add_class_to_wp_nav_menu', 10, 2 );
/** Check if Page has Children **/
function has_children( $pid ) {
$children = get_pages( "child_of={$pid}" );
return ( $children );
}