简体   繁体   中英

Wordpress Post ID in wp_nav_menu?

I'm looking to add the post ID to the markup of wp_nav_menu but not sure how to approach it.

I've found this:http://wordpress.org/support/topic/output-the-post-id-of-wp_nav_menu-items

which suggests this, but putting it in my functions.php file doesnt do any thing.

// get menu item's content id
     class pages_from_nav extends Walker_Nav_Menu
      {
            function start_el(&$output, $item, $depth, $args)
            {
                 global $wp_query;
                            $item_output .= $item->object_id;
                            $item_output .= ',';
                  $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
                  }
      }

Any ideas on how I could get the post ID as either a class, an ID or a data attribute?

Have it figured out via some nice chaps on the WP forums.

Needed to create a walker, which is a custo nav structure:

// nav menu walker
class My_Walker extends Walker_Nav_Menu
{
    function start_el(&$output, $item, $depth, $args) {
        global $wp_query;
        $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';

        $class_names = $value = '';

        $classes = empty( $item->classes ) ? array() : (array) $item->classes;

        $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );
        $class_names = ' class="' . esc_attr( $class_names ) . '"';

        $output .= $indent . '<li id="menu-item-'. $item->ID . '"' . $value . $class_names .'>';

        $attributes  = ! empty( $item->attr_title ) ? ' title="'  . esc_attr( $item->attr_title ) .'"' : '';
        $attributes .= ! empty( $item->target )     ? ' target="' . esc_attr( $item->target     ) .'"' : '';
        $attributes .= ! empty( $item->xfn )        ? ' rel="'    . esc_attr( $item->xfn        ) .'"' : '';
        $attributes .= ! empty( $item->url )        ? ' href="'   . esc_attr( $item->url        ) .'"' : '';
        $attributes .= ' data-id="'. esc_attr( $item->object_id        ) .'"';
        $attributes .= ' data-slug="'. esc_attr(  basename(get_permalink($item->object_id )) ) .'"';



        $item_output = $args->before;
        $item_output .= '<a'. $attributes .'>';
        $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
        $item_output .= '</a>'; /* This is where I changed things. */
        $item_output .= $args->after;

        $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
    }
}

Then just pass the walker into my menu when I call wp_nav_menu();

        <?php        
$walker = new My_Walker;
    wp_nav_menu(array(
       'echo' => true,
       'menu' => 4,
        'container' => '',
        'theme_location' => 'primary',
        'menu_class' => 'grid-10 omega',
        'walker' => $walker
    )); ?>

Seems like this was over complicated. All you really need is url_to_postid(); where the URL you pass will be the link for the menu item.

add_filter('nav_menu_link_attributes', 'menu_post_ids');
function menu_post_ids($val){
 $postid = url_to_postid( $val['href'] );
 $val['data-postid'] = $postid;
 return $val;
}

You could also use this to query data from the post such as a featured image, etc., and further manipulate the menu on the front end using jQuery and whatever post related attributes you added.

add_filter('nav_menu_link_attributes', 'my_menu_images');
function my_menu_images($val){
 $postid = url_to_postid( $val['href'] );
 $val['data-image'] = get_the_post_thumbnail_url($postid);
 return $val;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM