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 have a site with a custom post type “projects” and two taxonomies “clients” and “mediums”. For the navigation I’m using a custom nav walker (that extends Walker_Nav_Menu). This all works very well and allows URLs such as:

/mediums/print/ (showing selection of print projects) /clients/clientA (showing selection of projects for clientA)

I have page templates for clients and mediums and templates taxonomy-clients.php and taxonomy-mediums.php. Ideally I would like to be able to have both those URLs and the nav continue to select an individual project through either client or medium e.g.

/mediums/print/project-A /clients/clientA/project-A

But after a very long search and searching way too many forums it seems this isn’t possible with Wordpress. Therefore I had to go with one path and chose:

/mediums/print/project-A

To allow this to work and not just display all single projects post under /projects/project-A

I’m adding a filter hook to 'post_type_link' which modifies links to follow the full structure of /mediums/print/project-A and in the custom post type I’m using a placeholder %mediums% which is replaced by the relevant taxonomy term.

Again, this all works very well and now allows browsing by client or medium and selecting the project which then selects the full URL.

The problem I’m having is now the navigation doesn’t show any state for the single-projects.php template (nav is global and in the sidebar). I can see that this is most likely because although I’ve managed to get the URL and the links to work showing the full path WP and hence the walker don’t see the custom post type project as sitting below mediums/mediums-term and so doesn’t create the nav items required to show this state.

Does anyone know how I can get the walker to “see” this relationship?

I don’t think it’s unreasonable for users to want to selectively drill down to a project in this manner through project taxonomy and land on the project at the end of the tree rather than just jumping out to /projects/project-A with no reflection of the state in the navigation.

Thanks.

share|improve this question

1 Answer 1

I don't understand exactly why you chose to use a custom Nav Walker and how you managed to make it work. You should use this when you want to display the menu entries with a very specific html code that do not fit into the wp_nav_menu output. In your case, you want to change the link itself, not only the html formatting.

I think your request can be solved in an easier manner by using a rewrite rule. That rule will transform any custom formatted URL to another that matches the default Wordpress format.

If I understand well you need, you will have to add the following rules :

add_rewrite_rule('^mediums/([^/]*)/([^/]*)/?','index.php?mediums=$matches[1]&project=$matches[2]','top');
add_rewrite_rule('^clients/([^/]*)/([^/]*)/?','index.php?clients=$matches[1]&project=$matches[2]','top');

Add this code to the init hook, and you should be able to achieve the desired result.

Now, to display the navigation menu, you should use the get_terms function to list your taxonomies terms and display the appropriated links.

share|improve this answer

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.