Support » Fixing WordPress » Get Children and Loop Through Each Child

  • After endless research with no joy I find myself having to ask for help. Below is what I would like to do:

    Query the child pages from a specified parent page (id=7). For each child page, display an custom field (image) which links to the child page (its permalink)

    Anybody any ideas – it sounds easy but I have not had any joy so far. Thanks in advance.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter equaldesign


    Typical! As soon as I post here I find the answer myself. Below does the above, but brings the title of each post rather than an image:

    <?php query_posts(array('showposts' => 20, 'post_parent' => 7, 'post_type' => 'page')); while (have_posts()) { the_post(); ?>
    <h2><?php the_title(); ?></h2>
    <?php } ?>

    Well thanks for posting your finding. You helped me immensely!! 🙂

    How can you do this with categories?

    So that the output is a list of child categories, and the posts from each one.

    for child categories:

    $cats = get_categories('child_of=7&orderby=count&order=DESC'); 
    foreach ($cats as $cat) :
      $args = array(
        'posts_per_page' => -1, // max number of post per child category
        'cat' => $cat->term_id
      if (have_posts()) : 
    echo $cat->name; //optional
        while(have_posts()) :
          /*here what you want to output*/

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Get Children and Loop Through Each Child’ is closed to new replies.