Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save samuelcotterall/b252160874b99f4faee65a25fa1b4316 to your computer and use it in GitHub Desktop.
Save samuelcotterall/b252160874b99f4faee65a25fa1b4316 to your computer and use it in GitHub Desktop.

Revisions

  1. samuelcotterall revised this gist Jan 7, 2017. 1 changed file with 1 addition and 1 deletion.
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@

    // Getting the post type of the current post
    $current_post_type = get_post_type_object(get_post_type($post->ID));
    $current_post_type_slug = $current_post_type->rewrite[slug];
    $current_post_type_slug = $current_post_type->rewrite['slug'];

    // Getting the URL of the menu item
    $menu_slug = strtolower(trim($item->url));
  2. Gerben van Dijk renamed this gist Jun 12, 2013. 1 changed file with 0 additions and 0 deletions.
  3. Gerben van Dijk renamed this gist Mar 27, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. Gerben van Dijk revised this gist Mar 27, 2013. No changes.
  5. Gerben van Dijk revised this gist Mar 27, 2013. No changes.
  6. Gerben van Dijk created this gist Mar 27, 2013.
    29 changes: 29 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    <?php

    add_action('nav_menu_css_class', 'add_current_nav_class', 10, 2 );

    function add_current_nav_class($classes, $item) {

    // Getting the current post details
    global $post;

    // Getting the post type of the current post
    $current_post_type = get_post_type_object(get_post_type($post->ID));
    $current_post_type_slug = $current_post_type->rewrite[slug];

    // Getting the URL of the menu item
    $menu_slug = strtolower(trim($item->url));

    // If the menu item URL contains the current post types slug add the current-menu-item class
    if (strpos($menu_slug,$current_post_type_slug) !== false) {

    $classes[] = 'current-menu-item';

    }

    // Return the corrected set of classes to be added to the menu item
    return $classes;

    }

    ?>