Walker class for sub-menu with ACF fields

Firstly, I'm wondering if this is the best way to even do this.I have a basic nav, one item has children.The childr

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 badges
Add a comment  | 

1 Answer 1

Reset to default 2

Yes, 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745557892a4632929.html

相关推荐

  • Walker class for sub-menu with ACF fields

    Firstly, I'm wondering if this is the best way to even do this.I have a basic nav, one item has children.The childr

    19小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信