最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Walker class for sub-menu with ACF fields

programmeradmin1浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论