wp_setup_nav_menu_item

Decorates a menu item object with the shared navigation menu item properties.

Description

(object) wp_setup_nav_menu_item( (object) $menu_item ); 

Properties: - ID: The term_id if the menu item represents a taxonomy term. - attr_title: The title attribute of the link element for this menu item. - classes: The array of class attribute values for the link element of this menu item. - db_id: The DB ID of this item as a nav_menu_item object, if it exists (0 if it doesn't exist). - description: The description of this menu item. - menu_item_parent: The DB ID of the nav_menu_item that is this item's menu parent, if any. 0 otherwise. - object: The type of object originally represented, such as category, "post", or attachment. - object_id: The DB ID of the original object this menu item represents, e.g. ID for posts and term_id for categories. - post_parent: The DB ID of the original object's parent object, if any (0 otherwise). - post_title: A no title label if menu item represents a post that lacks a title. - target: The target attribute of the link element for this menu item. - title: The title of this menu item. - type: The family of objects originally represented, such as post_type or taxonomy. - type_label: The singular label used to describe this type of menu item. - url: The URL to which this menu item points. - xfn: The XFN relationship expressed in the link of this menu item. - _invalid: Whether the menu item represents an object that no longer exists.

Returns (object)

$menu_item The menu item with standard menu item properties.

Parameters (1)

0. $menu_item (object)
The menu item to modify.

Usage

  1. if ( !function_exists( 'wp_setup_nav_menu_item' ) ) { 
  2. require_once ABSPATH . WPINC . '/nav-menu.php'; 
  3.  
  4. // The menu item to modify. 
  5. $menu_item = null; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = wp_setup_nav_menu_item($menu_item); 
  9.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/nav-menu.php  
  1. function wp_setup_nav_menu_item( $menu_item ) { 
  2. if ( isset( $menu_item->post_type ) ) { 
  3. if ( 'nav_menu_item' == $menu_item->post_type ) { 
  4. $menu_item->db_id = (int) $menu_item->ID; 
  5. $menu_item->menu_item_parent = ! isset( $menu_item->menu_item_parent ) ? get_post_meta( $menu_item->ID, '_menu_item_menu_item_parent', true ) : $menu_item->menu_item_parent; 
  6. $menu_item->object_id = ! isset( $menu_item->object_id ) ? get_post_meta( $menu_item->ID, '_menu_item_object_id', true ) : $menu_item->object_id; 
  7. $menu_item->object = ! isset( $menu_item->object ) ? get_post_meta( $menu_item->ID, '_menu_item_object', true ) : $menu_item->object; 
  8. $menu_item->type = ! isset( $menu_item->type ) ? get_post_meta( $menu_item->ID, '_menu_item_type', true ) : $menu_item->type; 
  9.  
  10. if ( 'post_type' == $menu_item->type ) { 
  11. $object = get_post_type_object( $menu_item->object ); 
  12. if ( $object ) { 
  13. $menu_item->type_label = $object->labels->singular_name; 
  14. } else { 
  15. $menu_item->type_label = $menu_item->object; 
  16. $menu_item->_invalid = true; 
  17.  
  18. if ( 'trash' === get_post_status( $menu_item->object_id ) ) { 
  19. $menu_item->_invalid = true; 
  20.  
  21. $menu_item->url = get_permalink( $menu_item->object_id ); 
  22.  
  23. $original_object = get_post( $menu_item->object_id ); 
  24. /** This filter is documented in wp-includes/post-template.php */ 
  25. $original_title = apply_filters( 'the_title', $original_object->post_title, $original_object->ID ); 
  26.  
  27. if ( '' === $original_title ) { 
  28. /** translators: %d: ID of a post */ 
  29. $original_title = sprintf( __( '#%d (no title)' ), $original_object->ID ); 
  30.  
  31. $menu_item->title = '' == $menu_item->post_title ? $original_title : $menu_item->post_title; 
  32.  
  33. } elseif ( 'post_type_archive' == $menu_item->type ) { 
  34. $object = get_post_type_object( $menu_item->object ); 
  35. if ( $object ) { 
  36. $menu_item->title = '' == $menu_item->post_title ? $object->labels->archives : $menu_item->post_title; 
  37. $post_type_description = $object->description; 
  38. } else { 
  39. $menu_item->_invalid = true; 
  40. $post_type_description = ''; 
  41.  
  42. $menu_item->type_label = __( 'Post Type Archive' ); 
  43. $post_content = wp_trim_words( $menu_item->post_content, 200 ); 
  44. $post_type_description = '' == $post_content ? $post_type_description : $post_content; 
  45. $menu_item->url = get_post_type_archive_link( $menu_item->object ); 
  46. } elseif ( 'taxonomy' == $menu_item->type ) { 
  47. $object = get_taxonomy( $menu_item->object ); 
  48. if ( $object ) { 
  49. $menu_item->type_label = $object->labels->singular_name; 
  50. } else { 
  51. $menu_item->type_label = $menu_item->object; 
  52. $menu_item->_invalid = true; 
  53.  
  54. $term_url = get_term_link( (int) $menu_item->object_id, $menu_item->object ); 
  55. $menu_item->url = !is_wp_error( $term_url ) ? $term_url : ''; 
  56.  
  57. $original_title = get_term_field( 'name', $menu_item->object_id, $menu_item->object, 'raw' ); 
  58. if ( is_wp_error( $original_title ) ) 
  59. $original_title = false; 
  60. $menu_item->title = '' == $menu_item->post_title ? $original_title : $menu_item->post_title; 
  61.  
  62. } else { 
  63. $menu_item->type_label = __('Custom Link'); 
  64. $menu_item->title = $menu_item->post_title; 
  65. $menu_item->url = ! isset( $menu_item->url ) ? get_post_meta( $menu_item->ID, '_menu_item_url', true ) : $menu_item->url; 
  66.  
  67. $menu_item->target = ! isset( $menu_item->target ) ? get_post_meta( $menu_item->ID, '_menu_item_target', true ) : $menu_item->target; 
  68.  
  69. /** 
  70. * Filters a navigation menu item's title attribute. 
  71. * @since 3.0.0 
  72. * @param string $item_title The menu item title attribute. 
  73. */ 
  74. $menu_item->attr_title = ! isset( $menu_item->attr_title ) ? apply_filters( 'nav_menu_attr_title', $menu_item->post_excerpt ) : $menu_item->attr_title; 
  75.  
  76. if ( ! isset( $menu_item->description ) ) { 
  77. /** 
  78. * Filters a navigation menu item's description. 
  79. * @since 3.0.0 
  80. * @param string $description The menu item description. 
  81. */ 
  82. $menu_item->description = apply_filters( 'nav_menu_description', wp_trim_words( $menu_item->post_content, 200 ) ); 
  83.  
  84. $menu_item->classes = ! isset( $menu_item->classes ) ? (array) get_post_meta( $menu_item->ID, '_menu_item_classes', true ) : $menu_item->classes; 
  85. $menu_item->xfn = ! isset( $menu_item->xfn ) ? get_post_meta( $menu_item->ID, '_menu_item_xfn', true ) : $menu_item->xfn; 
  86. } else { 
  87. $menu_item->db_id = 0; 
  88. $menu_item->menu_item_parent = 0; 
  89. $menu_item->object_id = (int) $menu_item->ID; 
  90. $menu_item->type = 'post_type'; 
  91.  
  92. $object = get_post_type_object( $menu_item->post_type ); 
  93. $menu_item->object = $object->name; 
  94. $menu_item->type_label = $object->labels->singular_name; 
  95.  
  96. if ( '' === $menu_item->post_title ) { 
  97. /** translators: %d: ID of a post */ 
  98. $menu_item->post_title = sprintf( __( '#%d (no title)' ), $menu_item->ID ); 
  99.  
  100. $menu_item->title = $menu_item->post_title; 
  101. $menu_item->url = get_permalink( $menu_item->ID ); 
  102. $menu_item->target = ''; 
  103.  
  104. /** This filter is documented in wp-includes/nav-menu.php */ 
  105. $menu_item->attr_title = apply_filters( 'nav_menu_attr_title', '' ); 
  106.  
  107. /** This filter is documented in wp-includes/nav-menu.php */ 
  108. $menu_item->description = apply_filters( 'nav_menu_description', '' ); 
  109. $menu_item->classes = array(); 
  110. $menu_item->xfn = ''; 
  111. } elseif ( isset( $menu_item->taxonomy ) ) { 
  112. $menu_item->ID = $menu_item->term_id; 
  113. $menu_item->db_id = 0; 
  114. $menu_item->menu_item_parent = 0; 
  115. $menu_item->object_id = (int) $menu_item->term_id; 
  116. $menu_item->post_parent = (int) $menu_item->parent; 
  117. $menu_item->type = 'taxonomy'; 
  118.  
  119. $object = get_taxonomy( $menu_item->taxonomy ); 
  120. $menu_item->object = $object->name; 
  121. $menu_item->type_label = $object->labels->singular_name; 
  122.  
  123. $menu_item->title = $menu_item->name; 
  124. $menu_item->url = get_term_link( $menu_item, $menu_item->taxonomy ); 
  125. $menu_item->target = ''; 
  126. $menu_item->attr_title = ''; 
  127. $menu_item->description = get_term_field( 'description', $menu_item->term_id, $menu_item->taxonomy ); 
  128. $menu_item->classes = array(); 
  129. $menu_item->xfn = ''; 
  130.  
  131.  
  132. /** 
  133. * Filters a navigation menu item object. 
  134. * @since 3.0.0 
  135. * @param object $menu_item The menu item object. 
  136. */ 
  137. return apply_filters( 'wp_setup_nav_menu_item', $menu_item );