• I have a secondary sidebar navigation on my site that is a combination of posts and pages. I have my css for current_page_item and it works great when you are on one of the pages, but it does not work if you are on one of the single post pages.

    I know I should be able to write a function so that I can assign the current_page_item class to the single post links in the navigation as well. I have found some resources for doing this with custom post types; however, I am not using custom post types and I was unable to hack those solutions and get them to work for me.

    I am sure I missing something very simple and obvious to do this, but I have spent all day going down the rabbit hole of links and forum posts, so any help would be greatly appreciated.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator bcworkz

    (@bcworkz)

    The specifics depend on how the menu is generated. Hopefully there is a filter or some way to add a class to specific menu items and that filter somehow makes the ID of the menu item’s target available to your callback. Given that, you can compare the results of get_queried_object_id() to the target’s ID. If there’s a match, the item’s target is the current page and a special class should be added to the item.

    This does not help if the target is something like a category archive, which has no ID to match against. You could determine what the current query is in most cases with get_queried_object(), but how to match that up with the menu item target I’m not sure. You’d need to output all the data available to your callback and see what you can match up.

    Check to see if you’re also getting a ‘current-menu-item’ class on your menus. WordPress core adds this to menus, including custom menus in widgets. You can style that instead of ‘currnet_page_item’.

    Thanks for the help, bcworkz, that took me down a much more sensible track than the one I was on…

    Using: $post_name = get_queried_object()->post_name; to collect some suitable information about the current page. This allows me to compare the post_name with each sidebar navigation item, choosing a suitable field for the comparison, so that when a match is made a new class name can be inserted, and hence modified in css.

    e.g.

    if (isset($menu['menu_item_title']) 
        && !empty($menu['menu_item_title']) 
        && $post_name == $menu['menu_item_title']) { ?>           
            <span class='icon-block icon-current'>
    <?php } else { ?>  
            <span class='icon-block'>
    <?php }; ?>

    Thanks again.

    • This reply was modified 7 years, 4 months ago by jw360.
    Thread Starter jacquemae

    (@jacquemae)

    Yes, I had tried both current-menu-item and current_page_item without success. I think short term I will just recreate the two posts that have been deemed worthy of being added to the navigation into pages. I appreciate all the comments and help.

    Hi jacquemae,
    The point here is that just altering the css in your theme (in style.css say, or similar) to give new colouration to current-menu-item, will not change the appearance of your secondary sidebar.

    You first of all need to insert a class for the current menu item in the code for that secondary sidebar. So, code similar to that given above – but matching the exact structure of your menu – has to be included in the secondary sidebar. Otherwise nothing happens!

    For others let me add that life is never quite so simple as might have been implied by the one-liner $post_name = get_queried_object()->post_name; in my earlier post! In this case because when the ‘page’ we reach is in fact the blog-posts category, get_queried_object() won’t contain a ‘post_name’, and when you click into a given blog post the post_name retrieved won’t match any menu item!
    So a more complex solution would be needed, such as…

    $post_name = ''; 
    $post_object = get_queried_object(); 
    if (isset($post_object->post_type)) {
      if ($post_object->post_type == 'page') {
        $post_name = $post_object->post_name;
      } else {
        if ($post_object->post_type == 'post') {
          $post_object = get_the_category($post_object->ID);
          $post_name = $post_object[0]->category_nicename;
        } else {
          // does this case exist?
        }
      }
    } else { 
      $post_name = $post_object->category_nicename;
    }
    unset($post_object);

    …bit sad really!

    • This reply was modified 7 years, 4 months ago by jw360.
Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘assigning current_page_item class to single post links in navigation’ is closed to new replies.