3

How can I remove parent slug from child page's permalink? This may not be Atahualpa specific, but I can't figure out how to do it... Perhaps there is a plugin that might work?

I use pages (i.e. not posts) that are setup as children of a parent page. In my menu bar navigation they appear as:

/services/page1.html
/services/page2.html
/services/page3.html

Where what I want is:

/page1.html
/page2.html
/page3.html

(I'm using the "html-on-pages" plugin to add the .html since I'm moving this site over from another server and this is the site structure it currently has.)

Is there any way to accomplish what I'm looking to do?

5 Answers 5

5

Add this to your functions.php

add_filter( 'post_link', 'remove_parent_cats_from_link', 10, 3 );
function remove_parent_cats_from_link( $permalink, $post, $leavename )
{
$cats = get_the_category( $post->ID );
if ( $cats ) {
// Make sure we use the same start cat as the permalink generator
usort( $cats, '_usort_terms_by_ID' ); // order by ID
$category = $cats[0]->slug;
if ( $parent = $cats[0]->parent ) {
// If there are parent categories, collect them and replace them in the link
$parentcats = get_category_parents( $parent, false, '/', true );
// str_replace() is not the best solution if you can have duplicates:
// myexamplesite.com/luxemburg/luxemburg/ will be stripped down to myexamplesite.com/
// But if you don't expect that, it should work
$permalink = str_replace( $parentcats, '', $permalink );
}
}
return $permalink;
}
1
  • For my purposes I had to use the page_link filter and perform slightly different logic, but this was pretty much what I was looking for!
    – tobek
    Apr 28, 2014 at 15:02
3

Try this Custom Permalinks plugin. This may help you.

1
  • Nice option. Thanks! Dec 5, 2017 at 21:35
0

Maybe Custom Permalinks plugin might help you.

The plugin sets custom permalinks on a per-post, per-tag or per-category basis.

0

It doesn't solve the issue of removing a parent slug from the URL, but it might solve the issue you have. It would require you to alter your theme and to utilize WordPress' native menu builder.

Create all your pages as normal, with the exception of nesting them under parents. Then create a new menu (Appearance > Menus) and add the "parent" pages. Using the menu builder, you can make top level pages appear nested under these "parent" pages. Once it's all done, set this menu as your primary menu. (this is all intuitive once you're on this menu builder page)

Lastly you would need to alter your theme to remove the name menu that it's using (likely wp_list_pages or something of the like) and the instead call in your new primary menu. You might use this code to call in that new menu:

< ?php wp_nav_menu( array( 'container_class' => 'menu-header', 'theme_location' => 'primary' ) ); ?>

Here's a more complete guide on accomplishing this.

FWIW, I'm trying to remove the parent slug from the url for a custom post type, so if you did, by chance, figure it out, please let me know. I'd be interested to see what you did to get this accomplished.

0

If someone needs a different hint:
If you want dropdown navigation menus using PARENT pages, then create a menu from admin dashboard, where you will set the dropdown menus using the PARENT PAGES.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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