Wordpress - 我如何知道菜单项是否有子项? [英] Wordpress - how do I know if a menu item has children?

查看:40
本文介绍了Wordpress - 我如何知道菜单项是否有子项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个带有嵌套子菜单的 wordpress 主题.我需要使没有孩子的元素在视觉上与有孩子的元素不同.现在我有这个菜单,但这可能会改变:

I'm developing a wordpress theme with nested submenus. I need to make the elements with no children visually different from the ones that have children. Right now I have this menu, but that could change:

A
  a1
  a2
B
  b1
  b2
C

如您所见,A 和 B 有孩子.C 没有 - 我需要它在 CSS 级别有所不同.

As you can see, A and B have children. C doesn't - I need it to be different in the CSS level.

理想情况下,我希望在 A 和 B 中有一个 has-children 类,但不在 C 中.

Ideally, I would like to have a has-children class in A and B, but not in C.

到目前为止,我已经成功创建了一个Menu Walker"PHP 类,我可以将其实例化并传递给 wp_nav_menu .它的构造函数看起来像这样:

So far I've managed to create a "Menu Walker" PHP class that I can instantiate and pass to wp_nav_menu . Its constructor looks like this:

class My_Walker_Nav_Menu extends Walker_Nav_Menu {
  function start_el(&$output, $item, $depth, $args) {
    ...
    if(??? $item has children???) {
      // I know what to do here
    }
  }
}

那么,我如何判断 $item 是否有孩子,或者是叶子?

So, how do I tell whether $item has children, or is a leaf?

这个问题是由 Wordpress 论坛中名为keesiemeijer"的人回答的.我要让这个赏金过期,以防他想收回它.否则,我会将自己的答案标记为有效.

this question was answered by someone called "keesiemeijer" in the Wordpress forums. I'm leaving this bounty expired just in case he wants to reclaim it. Otherwise, I'll be marking my own answer as valid.

推荐答案

我在 WordPress 论坛 和 keesiemeijer 将我指向 另一个帖子,他们在其中做了以下事情:

I asked in the WordPress forum and keesiemeijer pointed me to this other post, in which they did the following:

他们没有修改 start_el,而是修改了 display_element,添加了以下两行(第 37-38 行 此处):

Instead of modifying start_el, they modified display_element, adding the following two lines (lines 37-38 here):

//display this element (THESE ARE NOT THE LINES)
if ( is_array( $args[0] ) )
  $args[0]['has_children'] = ! empty( $children_elements[$element->$id_field] );

// THESE TWO ARE THE LINES:               
if( ! empty( $children_elements[$element->$id_field] ) )
  array_push($element->classes,'parent');

我将前两行留作空间参考,同时也作为对本文中其他答案的评论.似乎 wordpress 正在尝试"在 $args 中设置has_children"属性,但它要么做错了,要么以我不明白的方式.在任何情况下,has_children 参数永远不会传递给 start_el(请参阅 $args 这里)

I've left the previous two lines as a spacial reference, and also as a comment to other answers in this post. It seems that wordpress is "trying" to set a ´has_children´ property in $args, but it's either doing it wrong or in a way I don't understand. In any case, that has_children parameter is never passed down to start_el (see sample var_dump of an $args here)

这可能是我获得的 Wordpress 版本 (3.2.1) 上的一个错误,并且可能已在最新版本中修复.

This might be a bug on the Wordpress version I've got (3.2.1) and might have been fixed in the most recent version.

无论如何,我在 Wordpress 论坛上得到的答案是帮助我解决问题的答案,所以我认为这已经解决了.我会等待赏金到期,以防万一keesiemeijer 想把他的答案放在这里.

In any case, the answer I got in the Wordpress forum is the one that helped me fix it, so I consider this settled. I'll wait for the bounty to expire just in case keesiemeijer wants to put his answer here.

这篇关于Wordpress - 我如何知道菜单项是否有子项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆