• Gå direkte til primær navigation
  • Skip til indhold
  • Gå direkte til primær sidebar

Vayu Robins

WordPress theme & plugin developer

  • Kompetencer/Services
  • Om/About
  • Kontakt/Contact

Kode

Highlighting wp_nav_menu() ancestor with Children for custom post types

10. august 2011 by Vayu Robins 8 kommentarer

I was in need of a solution to highlight the top menu item for a custom post type when viewing a single subpage (cpt).  There is no built-in method in WordPress that adds a class to the top menu item of cpt, when viewing a child item.

I spent some time searching for this topic on the net and came across this post on WordPress Answers.  I specifically noticed somatics answer.

I had created a cpt called “trips”.  Because it was for a danish website I could not have a menu item named Trips.

I just didn’t like having to enter the slug into my Title Attribute, as trips makes no sense in danish and I didn’t want the browser tooltip to appear on the menu line on mouseover.  So I came up with idea of adding a class name of the cpt slug instead to the menu item either.

So I went into the Menus section and added this “trips-menu-item” to the css classes in the menu item.

The code below then finds the menu item with this class trips-menu-item and adds another “current_page_parent” class to it.  Furthermore, it removes the “current_page_parent” from the blog menu item, if this is present.

add_filter('nav_menu_css_class', 'current_type_nav_class', 10, 2);
function current_type_nav_class($classes, $item) {
    // Get post_type for this post
    $post_type = get_query_var('post_type');

    // Removes current_page_parent class from blog menu item
    if ( get_post_type() == $post_type )
        $classes = array_filter($classes, "get_current_value" );

    // Go to Menus and add a menu class named: {custom-post-type}-menu-item
    // This adds a current_page_parent class to the parent menu item
    if( in_array( $post_type.'-menu-item', $classes ) )
        array_push($classes, 'current_page_parent');

    return $classes;
}
function get_current_value( $element ) {
    return ( $element != "current_page_parent" );
}

 

Skrevet i: Kode, WordPress

WordPress coding standards

28. juli 2011 by Vayu Robins

Hvis du beslutter dig for at kode dit eget WordPress Theme, så husk endelig at gennemlæs denne side om coding standards inden du går i gang.  Jeg støder ofte på Themes hvor koden er noget rod og man skal bruge ekstra tid på at gennemskue hvad der egentlig sker i koden.  På siden står der kort og fint om de mest basale kode standarder og det tager ikke mere end et par minuter at gennemlæse det.

Skrevet i: Kode, WordPress

Primær Sidebar

Vayu

Vayu

I am a webdeveloper from Denmark. I make my living building WordPress websites, e-commece sites, custom themes, plugins and widgets.

Verified Services

View Full Profile →

Kategorier

  • Ikke kategoriseret
  • Kode
  • Plugin
  • WordPress
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Cookie Policy

  • vayu.dk
  • Frederikssundsvej 49
  • 2400 København NV
  • +45 61 77 00 33
  • info@vrweb.dk