Wordpress菜单:父项条件


Wordpress Menu: Condition on Parent Item

我有一个自定义菜单,它是由父-子子子级等层次结构构建的。在每个家长菜单项(只有六个)上,我都想在网站的角落里展示一张特别的图片。我现在需要一种方法来设置这样的条件(这当然是幻想代码):

if (current_parent_menu_item == "Home") : echo "Picture01"; endif;

if (current_parent_menu_item == "About") : echo "Picture02"; endif;

等等…

我不知道如何访问或询问当前的父菜单项。有人能帮忙吗?

非常感谢!Sebastian

对于这类东西,应该使用walker类。这里有一个简单的助行器类,你可以使用

class MyWalker extends Walker_Nav_Menu
{
    function display_element ($element, &$children_elements, $max_depth, $depth = 0, $args, &$output)
    {
        // check, whether there are children for the given ID and append it to the element with a (new) ID
        $element->hasChildren = isset($children_elements[$element->ID]) && !empty($children_elements[$element->ID]);
        return parent::display_element($element, $children_elements, $max_depth, $depth, $args, $output);
    }
    function start_el(&$output, $item, $depth, $args)
    {
        global $wp_query;
        // you know can access $item->hasChildren to do anything you want..
    }
}

如果您只是想克服它,那么您应该使用jquery将html添加到父元素中。你可以这样做。

jQuery(document).ready(){
   jQuery('ul#id > li').prepend('<img src="#" alt="">');
}