Support » Fixing WordPress » Modifying the behaviour of the WordPress Menu states

  • Resolved jmwhite19

    (@jmwhite19)


    This is a long shot but I’m developing a new theme and have hit a issue which I’m wondering if it’s possible to solve.

    For my navigation I’m taking advantage of the WordPress Menu feature. Everything works fine, I have a total of 6 navigation links 3 custom links 3 pages all assigned to the menu via the WordPress Menu editor, navigation code is completely dynamic and generated via wp_nav_menu. The nav links are:

    Home – Link
    About – Page
    Contact – Page
    Tutorials – Link
    Articles – Link
    Links – Page

    All of these nav links are top level (one level)

    So far everything works fine, structured correctly and displays fine, the states generated i.e. selected/active work great too and are styled slightly differently in my CSS to indicate an active page/link.

    Now the problem. The two link pages Tutorials and Articles have the URL path of mydomain.com/category/tutorials and mydomain.com/category/articles which pull all posts in those categories. Problem is once a post is clicked within that category the URL changes to my permalinks settings YYYY/MM/DD and the selected/active state is then lost due to it not being a page/link in the menu.

    My question is if its possible to modify the functionality of the WordPress Menu system to apply the active class generated on the tutorial or article top level link when say a post of that category is viewed? It seems a bit bespoke so I wondered if I could code up something in my functions.php to add additional behaviour to the Menu system.

    Thanks in advance,

    James

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter jmwhite19

    (@jmwhite19)

    I managed to find a solution to my situation.

    Basically I’ve learnt the WordPress Menu Feature with wp_nav_menu() is limited in respect to what I was after. Instead what can be done if you write the general nav structure manually and use wp_list_categories() and wp_list_pages() to display the required pages and categories on the nav.

    I came across this great post which details how to do it:

    http://www.pawelmartuszewski.co.uk/web_tutorials/create-advanced-wordpress-navigation-with-active-states-using-wp_pages-and-wp_categories-functions/

    On its own this method works well for pages. However for posts themselves to have its parent category active on the menu you need a plugin and need to modify it slightly. Again this is all detailed in the link above, but it works well.

    Thread Starter jmwhite19

    (@jmwhite19)

    The only thing I’d like to do now is add a fallback selected state if a certain request like the search page is made and no state is present on any of the nav links.

    Thread Starter jmwhite19

    (@jmwhite19)

    Got it. For a fallback class (in a way) you can use Conditional tags e.g.

    <li <?php if (is_home() || is_search()){echo 'class="current_page_item"';}?>><a>/">Home</a>

    This means if a page that doesn’t have a link on the nav, the home tab can be given the active state. In the above example, this includes the search page. You can use as many as required.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Modifying the behaviour of the WordPress Menu states’ is closed to new replies.