admin管理员组

文章数量:1122832

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!

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!

Share Improve this question asked Apr 8, 2015 at 16:03 jmpjmp 111 silver badge2 bronze badges 3
  • I had a similar situation recently. I added parent pages for structure sake, and then added a redirect to the first child page in case someone landed on the parent page by accident. – gdaniel Commented Apr 8, 2015 at 16:44
  • Yeah, it definitely works, I'm just not sure if a redirect is appropriate or not. Maybe this is just the way everyone accomplishes it...? – jmp Commented Apr 8, 2015 at 19:45
  • The only other way I can think of is rewriting URLs, but it seems to cause way more issues when you try to do that. – gdaniel Commented Apr 8, 2015 at 20:03
Add a comment  | 

1 Answer 1

Reset to default 0

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.

本文标签: url rewritingUse Parent Pages for URL Structure without Landing Page