-
Notifications
You must be signed in to change notification settings - Fork 10.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create URL for nested pages #3954
Comments
I've added this piece of code to my wordpress instance: function custom_rest() {
register_rest_field(array('page'), 'path', array(
'get_callback' => function ($post) {
return get_page_uri($post['id']);
}
));
}
add_action('rest_api_init', 'custom_rest'); This will add field |
I want to avoid adding additional code to my Wordpress instance. Nevertheless, thank you for your help :) |
If You want to do it entirely on gatsby side of things - you can: const _ = require(`lodash`);
exports.sourceNodes = ({ getNodes, boundActionCreators }) => {
const { createNodeField } = boundActionCreators;
const pageNodes = getNodes().filter(
node => node.internal.type === "wordpress__PAGE"
);
pageNodes.forEach(pageNode => {
let pathFragments = [];
let tmpNode = pageNode;
do {
pathFragments.push(tmpNode.slug);
tmpNode = pageNodes.find(
node => node.wordpress_id === tmpNode.wordpress_parent
);
} while (tmpNode);
const path = pathFragments.reverse().join("/");
createNodeField({
node: pageNode,
name: `path`,
value: path
});
});
}; and then you can query: query allPosts {
allWordpressPage {
edges {
node {
slug
title
fields {
path
}
}
}
}
} which will give you: {
"data": {
"allWordpressPage": {
"edges": [
{
"node": {
"slug": "test",
"title": "Test",
"fields": {
"path": "sample-page/test"
}
}
},
{
"node": {
"slug": "sample-page",
"title": "Sample Page",
"fields": {
"path": "sample-page"
}
}
}
]
}
}
} |
Perfect! That works for me :) |
Hi @pieh thanks for the example above. Maybe I'm missing something but I'm facing some problem in that the query returns all pages/nodes. What If I want to display children of a particular page and so on. Example Page A {child a, b c..}, Page B {child a, b, c}. How do I make this happen when a page is generated to show only the child pages associated to it? Thanks!! |
I figured a way to achieve this. For anyone interested, here's what you do: |
@batguyz maybe you could use something like this:
which should only return children of sample-page (if you have paths setup like that) edit: you would probably need to create and pass that glob in |
Thanks @pieh I'll try that out. I'll keep you posted, thanks a lot! |
Related, why doesn't this work?:
I would expect this query, which doesn't seem to be the case:
edit: nevermind, that did work – I needed to rebuild the site. Leaving up for posterity. Tangential, but you can also build a tree with something like the following:
It would be nice if there was an out of box solution for querying the entire tree in one go. |
boundActionCreators is now deprecated - I've modified the code with the new actions parameter
|
I've created nested pages in Wordpress (eg. example.com/parent_page/child_page) and I want to reflect same structure using Gatsby.
Want is a proper solution for that? I've tried to create a complex query but without success...
The text was updated successfully, but these errors were encountered: