wp_setup_nav_menu_item() – Decorates a menu item object with the shared navigation menu item properties.

You appear to be a bot. Output may be restricted

Description

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

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.

Usage

$object = wp_setup_nav_menu_item( $menu_item );

Parameters

$menu_item
( object ) required – The menu item to modify.

Returns

object The menu item with standard menu item properties.

Source

File name: wordpress/wp-includes/nav-menu.php


Lines:

1 to 100 of 205
function wp_setup_nav_menu_item( $menu_item ) {

  
/**
 * Filters whether to short-circuit the wp_setup_nav_menu_item() output.
 *
 * Returning a non-null value from the filter will short-circuit wp_setup_nav_menu_item(),
 * returning that value instead.
 *
 * @since 6.3.0
 *
 * @param object|null $modified_menu_item Modified menu item. Default null.
 * @param object      $menu_item          The menu item to modify.
 */
  $pre_menu_item = apply_filters( 'pre_wp_setup_nav_menu_item', null, $menu_item );

  if ( null !== $pre_menu_item ) {
    return $pre_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 = ! isset( $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        = ! isset( $menu_item->object_id ) ? get_post_meta( $menu_item->ID, '_menu_item_object_id', true ) : $menu_item->object_id;
      $menu_item->object           = ! isset( $menu_item->object ) ? get_post_meta( $menu_item->ID, '_menu_item_object', true ) : $menu_item->object;
      $menu_item->type             = ! isset( $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;
          // Denote post states for special pages (only in the admin).
          if ( function_exists( 'get_post_states' ) ) {
            $menu_post   = get_post( $menu_item->object_id );
            $post_states = get_post_states( $menu_post );
            if ( $post_states ) {
              $menu_item->type_label = wp_strip_all_tags( implode( ', ', $post_states ) );
            }
          }
        } else {
          $menu_item->type_label = $menu_item->object;
          $menu_item->_invalid   = true;
        }

        if ( 'trash' === get_post_status( $menu_item->object_id ) ) {
          $menu_item->_invalid = true;
        }

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

        if ( $original_object ) {
          $menu_item->url = get_permalink( $original_object->ID );
          
/** This filter is documented in wp-includes/post-template.php */
          $original_title = apply_filters( 'the_title', $original_object->post_title, $original_object->ID );
        } else {
          $menu_item->url      = '';
          $original_title      = '';
          $menu_item->_invalid = true;
        }

        if ( '' === $original_title ) {
          /* translators: %d: ID of a post. */
          $original_title = sprintf( __( '#%d (no title)' ), $menu_item->object_id );
        }

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

      } elseif ( 'post_type_archive' === $menu_item->type ) {
        $object = get_post_type_object( $menu_item->object );
        if ( $object ) {
          $menu_item->title      = ( '' === $menu_item->post_title ) ? $object->labels->archives : $menu_item->post_title;
          $post_type_description = $object->description;
        } else {
          $post_type_description = '';
          $menu_item->_invalid   = true;
        }

        $menu_item->type_label = __( 'Post Type Archive' );
        $post_content          = wp_trim_words( $menu_item->post_content, 200 );
        $post_type_description = ( '' === $post_content ) ? $post_type_description : $post_content;
        $menu_item->url        = get_post_type_archive_link( $menu_item->object );

      } 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;
        }

        $original_object = get_term( (int) $menu_item->object_id, $menu_item->object );

        if ( $original_object && ! is_wp_error( $original_object ) ) {
          $menu_item->url = get_term_link( (int) $menu_item->object_id, $menu_item->object );
          $original_title = $original_object->name;
        } else {
          $menu_item->url      = '';

 View on GitHub View on Trac