php - wp_nav_menu 中的 Wordpress 帖子 ID?

标签 php wordpress

我想将帖子 ID 添加到 wp_nav_menu 的标记中,但不确定如何处理它。

我找到了这个:http://wordpress.org/support/topic/output-the-post-id-of-wp_nav_menu-items

这暗示了这一点,但是将它放在我的 functions.php 文件中并没有做任何事情。

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

关于如何将帖子 ID 作为类、ID 或数据属性获取的任何想法?

最佳答案

通过 WP 论坛上的一些好人解决了这个问题。

需要创建一个 walker,这是一个 custo nav 结构:

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

然后在我调用 wp_nav_menu() 时将 walker 传递到我的菜单中;

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

关于php - wp_nav_menu 中的 Wordpress 帖子 ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6215797/

相关文章:

php - 基于付款方式的 WooCommerce 订单接收重定向

php - 选择 MAX CASE WHEN 查询

php - Cakephp 复杂查询构建

javascript - 将从数据库检索的记录显示到对话框中

html - 元素在悬停时跳来跳去

php - drupal 6 模块编程语义错误

php - 按 WooCommerce WC_Product_Query 中的产品属性术语过滤

jQuery:将值传递到字段联系表单 7

javascript - 如何让 chessboard.js 在本地 WordPress 上运行?

wordpress - 为什么安全锁在应用 SSL 证书后从我的 wordpress 网站上消失了?