OK I just started using WordPress not very good and I have a menu problem I can REALLY use some help please.

I am working on a website and it has multiple locations, and not all the locations sell the same products. All the locations are pages with a parent structure.

Ohio -Contact Us -About us -Products etc...

West Virginia -Contact Us -About us -Products etc...

Pennsylvania -Contact Us -About us -Products


I got the hang of custom page templates and adding a custom menu to them (so all my Ohio pages use a custom Ohio template i made and included a custom Ohio menu).

I am using posts to create the product information pages and display them using:

*post_type="post" taxonomy="category" posts_per_page="10" tax_term=*

which seems to work out nice on a general product page, (especially since i would like to only create the product pages once sine there are a lot of them) but i cant figure out how to add a custom menu based on the parent pages they came from.

So if they come from my custom Ohio page and click on the link to a product post, there is not menu since that product might also be in West Virginia but not in Pennsylvania, so i cant use a standard menu.

Any way to get the parent info from where they just came from or something?

Any thoughts?

I am really new at WordPress coding so please be gentle

  • Is this your question? Any way to get the parent info from where they just came from or something? Jan 20, 2014 at 3:24
  • Yea basically, i need to know if there is a way to see what parent them came from to build a custom menu based on that
    – Chris
    Jan 20, 2014 at 3:26

1 Answer 1


Can I confirm that you've actually created separate sub-pages for these duplicate products under each state?

Assuming that's true, you could use what I call a "soft-system" approach and use a naming convention. If you included Ohio in the page name (again, assuming post-name is part of your permalink structure, which is better for SEO anyway...), you could conditionally test for page name. this becomes a little more functional if your naming convention dictates that your product page will begin with the parent state.


then you have many ways to strip the significant portion of the uri prior to the first dash.

forgive my lack of code. I can always provide a basic example.

This is not the most Object Oriented approach, but if you're new to WP and you're not terrific with PHP my solution might be good for you.

Otherwise, try this:


Or, you could always use this WP function:


"Returns Array of IDs or empty if no ancestors are found. The direct parent is returned as the first value in the array. The highest level ancestor is returned as the last value in the array."

You would of course still end up with a conditional statement, such as:

$ancestry = get_post_ancestors( $post );
if( isset($ancestry) && !empty($ancestry)){
    $last = count($ancestry) - 1;
    $menu_per_state = get_the_title( $ancestry[ $last ] ); //uses ID 

    // relies on matching name for menu and page title using this method. 
    // uses default theme_location since none specified
    // refer to docs on wp_nav_menu, very helpful

    wp_nav_menu( array('menu' => $menu_per_state) );  

Your Answer

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

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