Skip to content

Instantly share code, notes, and snippets.

@nanlabsweb
Created March 20, 2018 19:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nanlabsweb/673dbcdb420a00ab65dfd77a89ea154b to your computer and use it in GitHub Desktop.
Save nanlabsweb/673dbcdb420a00ab65dfd77a89ea154b to your computer and use it in GitHub Desktop.
Main navigation menu using first level Wordpress pages.
import React from 'react';
import { Link, withRouter } from 'react-router-dom';
import { graphql } from 'react-apollo';
import gql from 'graphql-tag';
const Nav = (props) => {
const { loading, pages } = props.data;
if (loading) {
return (
<div>Loading...</div>
);
}
if (pages.items) {
return (
<nav>
{ pages.items.map(item => {
return (
<Link key={item.page.slug}
to={`/${item.page.slug}`}
className={(props.location.pathname == `/${item.page.slug}`) ? 'active' : '' }>
{item.page.title}
</Link>
);
}) }
</nav>
);
}
return (<div>No pages</div>);
};
export default graphql(gql`
query GetFirstLevelPages {
pages(first: 20, where: {
orderby: {
field: DATE,
order: ASC
}
}) {
items: edges {
page: node {
title
slug
}
}
}
}
`)(withRouter(Nav));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment