Skip to content

Instantly share code, notes, and snippets.

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

Revisions

  1. gerbenvandijk revised this gist Oct 12, 2020. 1 changed file with 34 additions and 34 deletions.
    68 changes: 34 additions & 34 deletions Mark parent navigation active when on custom post type single page
    Original file line number Diff line number Diff line change
    @@ -1,46 +1,46 @@
    <?php

    function add_current_nav_class($classes, $item) {
    function add_current_nav_class($classes, $item) {

    // Getting the current post details
    global $post;
    // Getting the current post details
    global $post;

    // Get post ID, if nothing found set to NULL
    $id = ( isset( $post->ID ) ? get_the_ID() : NULL );
    // Get post ID, if nothing found set to NULL
    $id = ( isset( $post->ID ) ? get_the_ID() : NULL );

    // Checking if post ID exist...
    if (isset( $id )){
    // Checking if post ID exist...
    if (isset( $id )){

    // Getting the post type of the current post
    $current_post_type = get_post_type_object(get_post_type($post->ID));
    // Getting the post type of the current post
    $current_post_type = get_post_type_object(get_post_type($post->ID));

    // Getting the rewrite slug containing the post type's ancestors
    $ancestor_slug = $current_post_type->rewrite ? $current_post_type->rewrite['slug'] : '';
    // Getting the rewrite slug containing the post type's ancestors
    $ancestor_slug = $current_post_type->rewrite ? $current_post_type->rewrite['slug'] : '';

    // Split the slug into an array of ancestors and then slice off the direct parent.
    $ancestors = explode('/',$ancestor_slug);
    $parent = array_pop($ancestors);
    // Split the slug into an array of ancestors and then slice off the direct parent.
    $ancestors = explode('/',$ancestor_slug);
    $parent = array_pop($ancestors);

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

    // Remove domain from menu slug
    $menu_slug = str_replace($_SERVER['SERVER_NAME'], "", $menu_slug);
    // Remove domain from menu slug
    $menu_slug = str_replace($_SERVER['SERVER_NAME'], "", $menu_slug);

    // If the menu item URL contains the post type's parent
    if (!empty($menu_slug) && !empty($parent) && strpos($menu_slug,$parent) !== false) {
    $classes[] = 'current-menu-item';
    }
    // If the menu item URL contains any of the post type's ancestors
    foreach ( $ancestors as $ancestor ) {
    if (!empty($menu_slug) && !empty($ancestor) && strpos($menu_slug,$ancestor) !== false) {
    $classes[] = 'current-page-ancestor';
    }
    }
    }
    // Return the corrected set of classes to be added to the menu item
    return $classes;
    // If the menu item URL contains the post type's parent
    if (!empty($menu_slug) && !empty($parent) && strpos($menu_slug,$parent) !== false) {
    $classes[] = 'current-menu-item';
    }

    // If the menu item URL contains any of the post type's ancestors
    foreach ( $ancestors as $ancestor ) {
    if (!empty($menu_slug) && !empty($ancestor) && strpos($menu_slug,$ancestor) !== false) {
    $classes[] = 'current-page-ancestor';
    }
    }
    }
    // Return the corrected set of classes to be added to the menu item
    return $classes;

    }
    add_action('nav_menu_css_class', 'add_current_nav_class', 10, 2 );
    }
    add_action('nav_menu_css_class', 'add_current_nav_class', 10, 2 );
  2. gerbenvandijk revised this gist Oct 12, 2020. 1 changed file with 44 additions and 27 deletions.
    71 changes: 44 additions & 27 deletions Mark parent navigation active when on custom post type single page
    Original file line number Diff line number Diff line change
    @@ -1,29 +1,46 @@
    <?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;

    }

    ?>
    function add_current_nav_class($classes, $item) {

    // Getting the current post details
    global $post;

    // Get post ID, if nothing found set to NULL
    $id = ( isset( $post->ID ) ? get_the_ID() : NULL );

    // Checking if post ID exist...
    if (isset( $id )){

    // Getting the post type of the current post
    $current_post_type = get_post_type_object(get_post_type($post->ID));

    // Getting the rewrite slug containing the post type's ancestors
    $ancestor_slug = $current_post_type->rewrite ? $current_post_type->rewrite['slug'] : '';

    // Split the slug into an array of ancestors and then slice off the direct parent.
    $ancestors = explode('/',$ancestor_slug);
    $parent = array_pop($ancestors);

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

    // Remove domain from menu slug
    $menu_slug = str_replace($_SERVER['SERVER_NAME'], "", $menu_slug);

    // If the menu item URL contains the post type's parent
    if (!empty($menu_slug) && !empty($parent) && strpos($menu_slug,$parent) !== false) {
    $classes[] = 'current-menu-item';
    }

    // If the menu item URL contains any of the post type's ancestors
    foreach ( $ancestors as $ancestor ) {
    if (!empty($menu_slug) && !empty($ancestor) && strpos($menu_slug,$ancestor) !== false) {
    $classes[] = 'current-page-ancestor';
    }
    }
    }
    // Return the corrected set of classes to be added to the menu item
    return $classes;

    }
    add_action('nav_menu_css_class', 'add_current_nav_class', 10, 2 );
  3. Gerben van Dijk renamed this gist Jun 12, 2013. 1 changed file with 0 additions and 0 deletions.
  4. Gerben van Dijk renamed this gist Mar 27, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. Gerben van Dijk revised this gist Mar 27, 2013. No changes.
  6. Gerben van Dijk revised this gist Mar 27, 2013. No changes.
  7. 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;

    }

    ?>