Some of the pages in my navigation are fragmented urls that shouldn't actually go anywhere. In other words, I have a couple parent nav items like <a href="#">Services</a> that serve only as a navigation item and not an actual landing page.

I know I can make a custom link in the menu to represent this, but if I do that, then the url of any child page under that page isn't created dynamically (i.e http://example.com/services/child-page). I have to manually change the url of that child page.

How can I accomplish this by creating pages rather than building custom links in menus?

  • You could use javascript to find any links with href="#" and automatically return false so that they go nowhere. – Howdy_McGee Dec 28 '15 at 21:04
  • Possible duplicate of How can I disable parent menu item links? – Howdy_McGee Dec 28 '15 at 21:04
  • I don't think I understand what you want? – s_ha_dum Dec 28 '15 at 21:28
  • In other words, I want to disable a page, but keep it in the navigation. Surprised there isn't a simple solution... – frshjb373 Dec 28 '15 at 21:43

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.

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.