2015-03-03 82 views
1

我有一个自定义菜单,由父 - 子 - 子 - 孩子等 - 层次结构。在每个父级菜单项(其中只有6个存在)上,我想在网站的角落显示一张特殊图片。 我现在需要一种方法来设置这样的条件(这当然是幻想码):WordPress的菜单:父项的条件

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

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

等等...

我不知道我如何访问或询问当前的父菜单项。 任何人都可以帮忙吗?

非常感谢! 塞巴斯蒂安

回答

0

你应该使用沃克类这种东西。这里是你可以使用

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="">'); 
}