Print a list of child pages for each mother page (4 posts)

  1. zamarrone
    Posted 5 years ago #

    Hi all (sorry for my bad english, but I'm italian :)

    My site has a structure like this one:


    - Drink
    - Eat

    - Apples
    - Bananas

    I would like to print in home page a <div> for each "mother page" containing a link to each child page. How could I do it automatically in the loop?

    I tried with the function "if have_posts" but i think i need something like "if have_pages"......

    Thank you very much in advance for any suggestion!
    Zam ;)

  2. MichaelH
    Posted 5 years ago #

    $parent_id = $posts[0]->ID;
      'post_parent' => $parent_id,
      'post_type' => 'page',
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'caller_get_posts'=> 1
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      echo 'List of Posts';
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        $meta = get_post_meta($post->ID, 'cf1', true);
        if ($meta){
          echo 'customfield1: '. $meta;
    wp_reset_query();  // Restore global post data stomped by the_post().
  3. zamarrone
    Posted 5 years ago #

    Hi MichaelH, thank you very much! Tomorrow I will try to integrate your code in my pages and i'll let you now.

    thanks again

  4. MichaelH
    Posted 5 years ago #

    Actually rereading your question, that code won't work as it only returns the child pages of the current page.

    You'll have to use Function_Reference/get_pages with the parent=0 argument to return the top level pages, then iterate though that array to display the parent page, then get the children of that parent page with the child_of=x argument.

Topic Closed

This topic has been closed to new replies.

About this Topic