You can modify the href
attribute of specific menu items when the menu is generated.
function wpd_convert_menu_items_to_hash( $atts, $item, $args ) {
if( $somecondition ){
$atts['href'] = '#';
}
return $atts;
}
add_filter( 'nav_menu_link_attributes', 'wpd_convert_menu_items_to_hash', 10, 3 );
In this example, $somecondition
is whatever way you're identifying these menu items. $item
gives you access to the menu item object, which contains the ID of the page and the slug, among other things. You can also look at $atts['href']
, which will be the default URL value for that page. $args
will give you access to the nav menu arguments, so you can target the whole thing to a specific menu.
You could also hook template_redirect
and send requests for those pages elsewhere.
function wpd_check_if_page_should_be_visible(){
if( is_page( 'services' ) ){
wp_redirect( home_url('/services/some-other-page/') );
}
}
add_action( 'template_redirect', 'wpd_check_if_page_should_be_visible' );
You could modify the above to check for a value in post meta, so the slug isn't hardcoded. You could also query for the children and forward to a child page, so the destination wouldn't have to be dynamic.
href="#"
and automaticallyreturn false
so that they go nowhere.