Wordpress recursive page listing


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

this is pretty useful if you want to use the wordpress blog system to feed a custom application/site of yours


Copy this code and paste it in your HTML
  1. to test it, put this in "wp-content/themes/[theme name]/header.php"
  2.  
  3.  
  4. //-------------------------------------------------------------
  5. // pagesRecursive
  6. // lists pages of a WP recursively
  7. //-------------------------------------------------------------
  8. function pagesRecursive($parentId, $lvl){
  9. $args=array('child_of' => $parentId, 'parent' => $parentId);
  10. $pages = get_pages($args);
  11. if ($pages) {
  12. $lvl ++;
  13. foreach ($pages as $page) {
  14. print "<div style='margin-left:".($lvl * 30)."; border:solid 1px #000; margin-bottom:10px; '>";
  15. print $page->ID."<br>";
  16. print $page->post_date."<br>";
  17. print $page->post_title."<br>";
  18. print $page->post_content."<br>";
  19. print "</div>";
  20. pagesRecursive($page->ID, $lvl);
  21. }
  22. }
  23. }
  24. pagesRecursive(20, 0);

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.