Advertisements

wp_setup_nav_menu_item

Definition:
function wp_setup_nav_menu_item( $menu_item ) {}

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

Parameters

  • object $menu_item: The menu item to modify.

Return values

returns:The menu item with standard menu item properties.

Defined filters

  • nav_menu_attr_title
    apply_filters( 'nav_menu_attr_title', $menu_item->post_excerpt )
  • nav_menu_description
    apply_filters( 'nav_menu_description', wp_trim_words( $menu_item->post_content, 200 )
  • nav_menu_attr_title
    apply_filters( 'nav_menu_attr_title', '' )
  • nav_menu_description
    apply_filters( 'nav_menu_description', '' )
  • wp_setup_nav_menu_item
    apply_filters( 'wp_setup_nav_menu_item', $menu_item )

Source code

function wp_setup_nav_menu_item( $menu_item ) {

	if ( isset( $menu_item->post_type ) ) {

		if ( 'nav_menu_item' == $menu_item->post_type ) {

			$menu_item->db_id = (int) $menu_item->ID;

			$menu_item->menu_item_parent = empty( $menu_item->menu_item_parent ) ? get_post_meta( $menu_item->ID, '_menu_item_menu_item_parent', true ) : $menu_item->menu_item_parent;

			$menu_item->object_id = empty( $menu_item->object_id ) ? get_post_meta( $menu_item->ID, '_menu_item_object_id', true ) : $menu_item->object_id;

			$menu_item->object = empty( $menu_item->object ) ? get_post_meta( $menu_item->ID, '_menu_item_object', true ) : $menu_item->object;

			$menu_item->type = empty( $menu_item->type ) ? get_post_meta( $menu_item->ID, '_menu_item_type', true ) : $menu_item->type;



			if ( 'post_type' == $menu_item->type ) {

				$object = get_post_type_object( $menu_item->object );

				if ( $object ) {

					$menu_item->type_label = $object->labels->singular_name;

				} else {

					$menu_item->type_label = $menu_item->object;

					$menu_item->_invalid = true;

				}



				$menu_item->url = get_permalink( $menu_item->object_id );



				$original_object = get_post( $menu_item->object_id );

				$original_title = $original_object->post_title;

				$menu_item->title = '' == $menu_item->post_title ? $original_title : $menu_item->post_title;



			} elseif ( 'taxonomy' == $menu_item->type ) {

				$object = get_taxonomy( $menu_item->object );

				if ( $object ) {

					$menu_item->type_label = $object->labels->singular_name;

				} else {

					$menu_item->type_label = $menu_item->object;

					$menu_item->_invalid = true;

				}



				$term_url = get_term_link( (int) $menu_item->object_id, $menu_item->object );

				$menu_item->url = !is_wp_error( $term_url ) ? $term_url : '';



				$original_title = get_term_field( 'name', $menu_item->object_id, $menu_item->object, 'raw' );

				if ( is_wp_error( $original_title ) )

					$original_title = false;

				$menu_item->title = '' == $menu_item->post_title ? $original_title : $menu_item->post_title;



			} else {

				$menu_item->type_label = __('Custom');

				$menu_item->title = $menu_item->post_title;

				$menu_item->url = empty( $menu_item->url ) ? get_post_meta( $menu_item->ID, '_menu_item_url', true ) : $menu_item->url;

			}



			$menu_item->target = empty( $menu_item->target ) ? get_post_meta( $menu_item->ID, '_menu_item_target', true ) : $menu_item->target;



			$menu_item->attr_title = empty( $menu_item->attr_title ) ? apply_filters( 'nav_menu_attr_title', $menu_item->post_excerpt ) : $menu_item->attr_title;



			if ( empty( $menu_item->description ) )

				$menu_item->description = apply_filters( 'nav_menu_description',  wp_trim_words( $menu_item->post_content, 200 ) );



			$menu_item->classes = empty( $menu_item->classes ) ? (array) get_post_meta( $menu_item->ID, '_menu_item_classes', true ) : $menu_item->classes;

			$menu_item->xfn = empty( $menu_item->xfn ) ? get_post_meta( $menu_item->ID, '_menu_item_xfn', true ) : $menu_item->xfn;

		} else {

			$menu_item->db_id = 0;

			$menu_item->menu_item_parent = 0;

			$menu_item->object_id = (int) $menu_item->ID;

			$menu_item->type = 'post_type';



			$object = get_post_type_object( $menu_item->post_type );

			$menu_item->object = $object->name;

			$menu_item->type_label = $object->labels->singular_name;



			$menu_item->title = $menu_item->post_title;

			$menu_item->url = get_permalink( $menu_item->ID );

			$menu_item->target = '';



			$menu_item->attr_title = apply_filters( 'nav_menu_attr_title', '' );

			$menu_item->description = apply_filters( 'nav_menu_description', '' );

			$menu_item->classes = array();

			$menu_item->xfn = '';

		}

	} elseif ( isset( $menu_item->taxonomy ) ) {

		$menu_item->ID = $menu_item->term_id;

		$menu_item->db_id = 0;

		$menu_item->menu_item_parent = 0;

		$menu_item->object_id = (int) $menu_item->term_id;

		$menu_item->post_parent = (int) $menu_item->parent;

		$menu_item->type = 'taxonomy';



		$object = get_taxonomy( $menu_item->taxonomy );

		$menu_item->object = $object->name;

		$menu_item->type_label = $object->labels->singular_name;



		$menu_item->title = $menu_item->name;

		$menu_item->url = get_term_link( $menu_item, $menu_item->taxonomy );

		$menu_item->target = '';

		$menu_item->attr_title = '';

		$menu_item->description = get_term_field( 'description', $menu_item->term_id, $menu_item->taxonomy );

		$menu_item->classes = array();

		$menu_item->xfn = '';



	}



	return apply_filters( 'wp_setup_nav_menu_item', $menu_item );

}

4095

Advertisements

3 Responses to “wp_setup_nav_menu_item”

  1. Regarding the :

    nav_menu_description
    apply_filters( ‘nav_menu_description’, $menu_item->post_content )

    How would I stop the post content automatically being added to the menu description if its blank?

    Im using the descriptions in a theme ive developed and its really annoying having to delete the post content from the description each time I add a menu item 😦

    • You could add something like this to the functions.php file of your theme:

      add_filter( 'nav_menu_description', 'my_nav_menu_desc' );
      function my_nav_menu_desc( $content ) {
         global $post;
         // get the description of the post
         // if description is not blank return description
         // else return $content 
      }
      

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: