0

I have a page hierarchy that looks like this:

  • Top level page
    • Child page
      • Grandchild page
      • Grandchild page
      • Grandchild page
    • Child page
      • Grandchild page
      • Grandchild page
      • Grandchild page

On my top level page I want to display a specific child page, and in this child page I want to display the content from the grandchild pages.

All of this needs to be dynamic - i.e. if I add another grandchild page, this needs to show up on the top level page.

Can anyone please help me with this one, or point me in the right direction?

1 Answer 1

0

From your question I understand that inside the top level page you want to indicate which child page to include. The obvious way to do this is using a shortcode. So in your functions.php you would have something like this:

add_shortcode( 'insert_child', 'wpse240522_insert_child' );

In the content of your top level page you would have [insert_child 23], where 23 is the ID of the child page.

Now you must define the function that evaluates the shortcode, first getting the child page and then looping through all the grandchild pages:

function wpse240522_insert_child ($child_id) {
   $child_page = get_post ($child_id);
   // do stuff with the child page
   $grandchild_pages = new WP_Query (array ('post_parent' => $child_id));
   foreach ($grandchild_pages as $grandchild_page) {
     // do stuff with grandchild page
     }
   }
2
  • Thanks! My aknowledge in php and wordpress is very low. so some further explanation could help :) On the child page (it's a custom page template). Should i just run the default wp loop, or do i need to call the grandchild on the child page? And in the function you posted, where it says "//Do stuff with grandchild", what should i define here? Something that calls the grandchild, or just the content? Thank you so much for your help so far!
    – Linsjb
    Sep 27, 2016 at 0:53
  • If your PHP and WP knowledge is too low to understand what you can do once you have the ID of a post, such as displaying its title and content, your best course of action is to find one of the many online tutorials.
    – cjbj
    Sep 27, 2016 at 8:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.