Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to replace all my child page URLs to include a # automatically

example: http://www.website.com/parentfoo/#childfoo

I tried using wp_setup_nav_menu_item to run through all the child menu items and add '#' to it but now $menu_item->post_name brings up the ID so I don't know how to replace the string correctly.

What I have so far:

function auto_hashtag( $menu_item ) {
    if ( ! is_admin() && $menu_item->post_parent > 0) {

        // url
        $url = $menu_item->url;
        // slug
        $slug = $menu_item->post_name;
        $menu_item->url = str_replace($slug, "#"+$slug, $url);
    }

    return $menu_item;
}
add_filter( 'wp_setup_nav_menu_item', 'auto_hashtag' );
share|improve this question
    
can you give some of the current HTML output you are getting? –  Douglas.Sesar Apr 14 at 19:49

2 Answers 2

This ended up doing the trick.

function auto_hashtag( $menu_item ) {
    if ( ! is_admin() && $menu_item->post_parent > 0) {

        // url
        $url = $menu_item->url;
        $menu_item->url = preg_replace('!/([^/]+)/$!', "/#\\1", $url);
    }

    return $menu_item;
}
add_filter( 'wp_setup_nav_menu_item', 'auto_hashtag' );
share|improve this answer

What you are trying to do seems like a bad idea; the # symbol is used at the end of a URL to denote an anchor position for the website to scroll to.

Take for example this very page with this link:

Add # to submenu URLs automatically

When clicked, it moved the screen down to the <div> tag with an id of post-form.

In the example you give http://www.website.com/parentfoo/#childfoo , the browser would go to the page 'parentfoo' and look for a HTML element with the id of childfoo.

It would be a bad idea to change all the default URL rewriting to account for your wanted url structure.

In other words, your Child page URLs would not be child page URLs any longer if you continue.

MAYBE I JUST DO NOT UNDERSTAND WHAT YOU ARE TRYING TO DO ??

share|improve this answer
    
That's what I need to do in the context of how my site is being built. I'm taking all the child pages and loading them in the parent as one long page. When you click on the subpages, you would scroll down the page. Like this: themes.opendept.net/retro-portfolio/#about-me Why I'm doing this, is so my client can make subpages, and it'll automatically setup the anchors for him without ever going into the admin nav. –  Tom Apr 14 at 19:42
1  
I see, let me take another look at your function. –  Douglas.Sesar Apr 14 at 19:48
    
I just posted a solution that I found to work –  Tom Apr 14 at 19:55
    
Glad you got it working –  Douglas.Sesar Apr 14 at 20:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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