admin管理员组文章数量:1022679
Firstly, I'm wondering if this is the best way to even do this.
I have a basic nav, one item has children.
The children need to be wrapped in a container div and each child needs to pull some ACF values from its associated page (image, ACF repeater values). The children are basically tiles with content + background images.
Is a walker class the best way to achieve this? The only thing I can manage to do is affect all nav items, I'm unsure of how to make it so that children items are affected and bring in the ACF values.
Thanks
Firstly, I'm wondering if this is the best way to even do this.
I have a basic nav, one item has children.
The children need to be wrapped in a container div and each child needs to pull some ACF values from its associated page (image, ACF repeater values). The children are basically tiles with content + background images.
Is a walker class the best way to achieve this? The only thing I can manage to do is affect all nav items, I'm unsure of how to make it so that children items are affected and bring in the ACF values.
Thanks
Share Improve this question asked Apr 24, 2019 at 12:27 PhillPhill 1971 gold badge1 silver badge10 bronze badges1 Answer
Reset to default 2Yes, if you want to keep using a WP nav menu, a walker is the right way to go.
To only affect a certain level (i.e. children, grandchildren, parent, etc.) you can use $depth
.
class wpseWalker extends Walker_Nav_Menu {
public function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0) {
// Check $depth - if it's 0 it's the top parent, 1 is a direct child
if($depth == 1) {
// Just temporarily, show all the item's properties
// You'll want to grab the ID from here and pull the ACF data
$output .= print_r($item, true);
}
}
}
You'll also want to check and make sure the ACF data exists, in case someone sets up a menu item that doesn't have that info.
Firstly, I'm wondering if this is the best way to even do this.
I have a basic nav, one item has children.
The children need to be wrapped in a container div and each child needs to pull some ACF values from its associated page (image, ACF repeater values). The children are basically tiles with content + background images.
Is a walker class the best way to achieve this? The only thing I can manage to do is affect all nav items, I'm unsure of how to make it so that children items are affected and bring in the ACF values.
Thanks
Firstly, I'm wondering if this is the best way to even do this.
I have a basic nav, one item has children.
The children need to be wrapped in a container div and each child needs to pull some ACF values from its associated page (image, ACF repeater values). The children are basically tiles with content + background images.
Is a walker class the best way to achieve this? The only thing I can manage to do is affect all nav items, I'm unsure of how to make it so that children items are affected and bring in the ACF values.
Thanks
Share Improve this question asked Apr 24, 2019 at 12:27 PhillPhill 1971 gold badge1 silver badge10 bronze badges1 Answer
Reset to default 2Yes, if you want to keep using a WP nav menu, a walker is the right way to go.
To only affect a certain level (i.e. children, grandchildren, parent, etc.) you can use $depth
.
class wpseWalker extends Walker_Nav_Menu {
public function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0) {
// Check $depth - if it's 0 it's the top parent, 1 is a direct child
if($depth == 1) {
// Just temporarily, show all the item's properties
// You'll want to grab the ID from here and pull the ACF data
$output .= print_r($item, true);
}
}
}
You'll also want to check and make sure the ACF data exists, in case someone sets up a menu item that doesn't have that info.
本文标签: Walker class for submenu with ACF fields
版权声明:本文标题:Walker class for sub-menu with ACF fields 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745557890a2155984.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论