如何确定wordpress菜单项是否处于活动状态?

时间:2015-12-04 18:46:11

标签: php wordpress menu menuitem objectid

我正在使用Wordpress创建菜单,我正在尝试使用object_id来确定菜单项是否处于活动状态。问题是,当我手动设置menu-item-object-id时,它会被它自己的ID覆盖。

我有一个创建菜单的功能,然后是另一个为每个菜单添加菜单项的功能:

function create_menu($menu) {

    foreach($item in $key => $menu['items']) {
        create_menu_item($item, $key, $menu['id']);
    }
}

function create_menu_item($item, $position, $menuID) {

    // i'll use a dynamic value for the object id, but not even hard coding it works
    $args = [
        'menu-item-position' => $position,
        'menu-item-title' =>  (isset($item['title'])) ? $item['title'] : null,
        'menu-item-classes' => (isset($item['classes'])) ? $item['classes'] : null,
        'menu-item-url' => (isset($item['url'])) ? $item['url'] : null,
        'menu-item-status' => (isset($item['status'])) ? $item['status'] : 'publish',
        'menu-item-parent-id' => (isset($item['parent'])) ? $item['parent'] : 0,
        'menu-item-object-id' => '5'
    ];

    wp_update_nav_menu_item($menuID, 0, $args);
}

menu-item-object-id属性外,一切似乎都按预期工作!我手动将其设置为对象的ID,但是当我使用wp_get_nav_menu_items()时,$post->object_id始终与其自己的ID $post->ID相同。

function get_menu() {

    global $post;

    $menu = wp_get_nav_menu_items('primary');

    foreach($menu as $item) {

        // $item->object_id should now be '5' but instead it is equal to $item->ID so I am unable to check if it's active

        if($item->object_id == $post->ID) {
            // item is active
        }
    }

    return $menu;
}

为什么我的menu-item-object-id财产被覆盖?

1 个答案:

答案 0 :(得分:0)

它在任何地方都不直观或无法记录,但解决方案是将var stopAnimation = false; var closeTimeoutHandle = null; function fluctuate(bar) { if(stopAnimation){ return; } var amplitude = Math.random() * 10; console.log(amplitude); var height = amplitude * 4; //Animate the equalizer bar repeatedly bar.animate({ height: height }, function() { fluctuate($(this)); }); } $("#btnStart").click(function(i) { stopAnimation = false; fluctuate($('.bar')); closeTimeoutHandle = setTimeout(function(){ stopAnimation = true; }, 10 * 1000); //10 seconds }); $("#btnStop").click(function(i) { stopAnimation = true; fluctuate($('.bar')); // clear the timeout so that if the user starts the animation again // it doesn't get stopped when the initial timeout gets called if(closeTimeoutHandle){ clearTimeout(closeTimeoutHandle); } }); 设置为post_type,将menu-item-type设置为menu-item-object

page

设置完这些属性后,设置$args['menu-item-type'] = 'post_type'; $args['menu-item-object'] = 'page'; $args['menu-item-object-id'] = $page->ID; 变得不必要了。 Wordpress将使用页面永久链接自动设置网址。