Get WordPress Submenus in Gatsby JS

ぐ巨炮叔叔 提交于 2021-02-08 07:34:43

问题


I am fiddling around with Gatsby JS using WP as the backend and so far so good. Now I was trying to pull in the menu which for main menu items works just as expected. What I can't really wrap my head around is how to get the submenus pulled in.

The only related thing I found was https://github.com/gatsbyjs/gatsby/issues/2426 which does give me the submenus if I log the data. Just can't get them to be pulled into the menu.

Here is my query in layouts/index.js:

export const query = graphql`
  query LayoutQuery {
    allWordpressWpApiMenusMenusItems {
      edges {
        node {
          name
          count
          items {
            order
            title
            url
            wordpress_children {
              wordpress_id
              title
              url
            }
          }
        }
      }
    }
}
`

This is my menu component:

class MainMenu extends Component {
  render(){

    const data = this.props.menu.allWordpressWpApiMenusMenusItems.edges["0"].node.items
    console.log(data)

    return(
      <div>
      <h1>Menu</h1>
      <ul>
        {data.map((item) =>
          <li key={item.object_slug}>
            <Link to={item.url}>
              {item.title}
            </Link>
          </li>
        )}
      </ul>
      </div>
    )
  }
}

export default MainMenu

I tried fiddling around with variations of

{item.children["0"].wordpress_children.title}

but just can't get it to work:/ Any ideas or pointers would be much appreciated!


回答1:


I just tested this, and your logic is sound all you need is another loop to display subitems. So in your MainMenu.js you can do something like this:

class MainMenu extends Component {
render() {

    const data = this.props.menu.allWordpressWpApiMenusMenusItems.edges[0].node.items
    console.log(data)
    return (
        <div>
            <h1>Main Menu</h1>
            <ul>
                {data.map((item) =>
                    <li key={item.object_slug}>
                        <Link to={item.url}>
                            {item.title}
                        </Link>
                        <ul>
                            {item.wordpress_children && item.wordpress_children.map((subitem) =>
                                <li key={item.wordpress_id}>
                                    <Link to={subitem.url}>
                                        {subitem.title}
                                    </Link>
                                </li>
                            )}
                        </ul>
                    </li>
                )}
            </ul>
        </div>
    )
}
}

This line is very important {item.wordpress_children && item.wordpress_children.map((subitem)

This will check if your menu item has subitems, and if it does it will map them and iterate through them.

I hope this works for you, I tested it and it works.



来源:https://stackoverflow.com/questions/50074029/get-wordpress-submenus-in-gatsby-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!