admin管理员组

文章数量:1277488

I would like to introduce page-hierarchy navigation in my WordPress blog using subpages. So each page may have sub-pages, but I'd like that when a visitor clicks on a page header he will be redirected to a specific subpage. For example, say my hierarchy is:

1. Movies
1.1 General
1.2 Westerns
1.3 Animated
2. Music
2.1 General
2.2 Classic
2.3 Country

What I want is that when someone clicks on "Movies" he will reach "Movies/General". Can this be achieved using plugins alone? If not, what's required?

I would like to introduce page-hierarchy navigation in my WordPress blog using subpages. So each page may have sub-pages, but I'd like that when a visitor clicks on a page header he will be redirected to a specific subpage. For example, say my hierarchy is:

1. Movies
1.1 General
1.2 Westerns
1.3 Animated
2. Music
2.1 General
2.2 Classic
2.3 Country

What I want is that when someone clicks on "Movies" he will reach "Movies/General". Can this be achieved using plugins alone? If not, what's required?

Share Improve this question edited Apr 29, 2011 at 7:43 Chris_O 20.6k5 gold badges62 silver badges96 bronze badges asked Apr 29, 2011 at 7:21 Roee AdlerRoee Adler 1752 silver badges9 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 5

Using WordPress custom nav menus create a menu and add the "General" page 2 times. The first as the parent and the second as the first child then add the rest of the pages as childs under the first General.

Change the title in the first general to movies then on the front end when a user clicks movies they will got to the general page.

The following worked like a charm, and better than the answer above. The problem with the answer above is that when viewing sub-sub-pages it was suddenly not possible to highlight the top menu section simply with css. With this solution you can keep the menu as it actually is. (http://www.wprecipes/wordpress-page-template-to-redirect-to-first-child-page)

To achieve this recipe, you have to create a page template. Create a new file and paste the following code in it:

<?php
/*
Template Name: Redirect To First Child
*/
if (have_posts()) {
  while (have_posts()) {
    the_post();
    $pagekids = get_pages("child_of=".$post->ID."&sort_column=menu_order");
    $firstchild = $pagekids[0];
    wp_redirect(get_permalink($firstchild->ID));
  }
}
?>

Save the file under the name redirect.php and upload it to the wp-content/themes/your-theme directory of your WordPress install. Once done, you can use the page template.

I did it in a more simple way than above.

Create a custom link. For the URL make it the URL of the "default" sub page you want it to be and the put the link text as whatever the page is that you want it to be called.

For your case above it would be URL www.yourwebsite/General and link text 'Movies'

If you want the default to go to Westerns then you would put the URL as www.yourwebsite/Westerns and the link text as 'Movies'.

Doing this will allow you to click on the parent page 'Movies' and direct you to General or Westerns respectively.

本文标签: menusDirecting a page to a default subpage