Advertisement
alchymyth

show max 5 random pages of the grandchild level

Apr 12th, 2011
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.22 KB | None | 0 0
  1. <ul><?php
  2. $page_ids = array();
  3.   foreach( get_pages() as $pages ) {
  4.   if(!$pages->post_parent) $page_ids[] = $pages->ID;
  5.   }
  6. foreach( $page_ids as $page_id ) :
  7.  
  8. $postid = $page_id;
  9. $args = array(
  10. 'post_type' => 'page',
  11. 'post_status' => 'publish',
  12. 'posts_per_page' => -1,
  13. 'post_parent' => $postid
  14. );
  15. $child_level = get_posts($args);
  16. if($child_level) :
  17. foreach($child_level as $child_page) :
  18.  
  19. $args = array(
  20. 'post_type' => 'page',
  21. 'post_status' => 'publish',
  22. 'posts_per_page' => -1,
  23. 'post_parent' => $child_page->ID
  24. );
  25. $grandchild_level = get_posts($args);
  26. if($grandchild_level) :
  27. foreach($grandchild_level as $grandchild_page) {
  28. $grandchild_ids[] = $grandchild_page->ID;
  29. }
  30. endif;
  31.  
  32. endforeach;
  33. endif;
  34.  
  35. endforeach; //ends 'foreach( $page_ids as $page_id ) :'
  36.  
  37. $args = array(
  38. 'post_type' => 'page',
  39. 'post_status' => 'publish',
  40. 'posts_per_page' => 2,
  41. 'orderby' => 'rand',
  42. 'post__in' => $grandchild_ids
  43. );
  44. $my_query = null;
  45. $my_query = new WP_Query($args);
  46. if( $my_query->have_posts() ) {
  47. while ($my_query->have_posts()) : $my_query->the_post(); ?>
  48. <li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
  49. <?php endwhile;
  50. }
  51. wp_reset_query();
  52.  
  53. ?></ul>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement