Get parent name from post->ID and display list of child pages from that.


/ Published in: PHP
Save to your folder(s)

Use getTopParentPostName() to get the parent slug. Then use get_ID_by_slug() to get the ID of it. The use wp_list_pages() to display the child pages :)


Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. // get parent ID
  4. function getTopParentPostName($myid)
  5. {
  6. $mypage = get_page($myid);
  7.  
  8. if ($mypage->post_parent == 0)
  9. {
  10. return $mypage->post_name;
  11. }
  12. else
  13. {
  14. return getTopParentPostName($mypage->post_parent);
  15. }
  16. }
  17.  
  18. // get ID of parent from slug
  19. function get_ID_by_slug($page_slug) {
  20. $page = get_page_by_path($page_slug);
  21. if ($page) {
  22. return $page->ID;
  23. } else {
  24. return null;
  25. }
  26. }
  27.  
  28. $args = array(
  29. 'echo' => 1,
  30. 'title_li' => '',
  31. 'child_of' => get_ID_by_slug(getTopParentPostName($post->ID)),
  32. 'sort_column' => 'menu_order, post_title');
  33.  
  34. ?>
  35. <?=wp_list_pages( $args );?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.