Use Parent Pages for URL Structure without Landing Page

So this is more a philosophical question than anything, and if I'm asking this in the wrong place, I'm happy to remove the post:

What's the best way to use a parent page for structure while keeping that page inaccessible to users? I'm running into two issues:

First, I want to use the parent page in the main menu so that I can take advantage of WordPress's current-menu-parent and like classes for styling purposes. I know that normally you can set a child page as a menu item and simply rename it to match it's parent, but then I lose the classes for styling the "parent" (sibling) menu item when on a sibling page.

Second, this parent page needs to be inaccessible to users, so that even if they modify the URL in their web address bar, they can't access the page and (possibly) are redirected to the first child page.

In the past I've either gone with the child-page-labeled-as-parent-in-menu route and then used jQuery to highlight the proper menu item, or I've used wp_redirect() to keep the user from the parent page. Both seem kind of hack-ish to me and the jQuery method doesn't allow for scaling or reordering pages as well as I'd like.

Does anyone have any suggestions? If using wp_redirect() is okay, should it be a 302 or 301 redirect? 302 seems better to me because then the website pages and be reordered and we won't face any issues with users having the redirection cached.

Anyway, I'm at a loss as to what the best practice is in this situation. Any advice would be appreciated!

Topic wp-redirect url-rewriting urls Wordpress

Category Web


It seems that you might be creating a parent page for the wrong reasons. There are other classes that will be added to identify the current menu item and you can also change that behavior using a menu walker.

The current menu item should have current-menu-item class added to it so you could leverage that in your CSS.

Also in the menu setup instead of addding a parent page create a Custom Link with URL set to # if what you're after is just creating the right menu hierarchy.

As for the redirect, you can accomplish the functionality you describe by editing the page template php without the need for a child-parent relationship.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.