WordPress.org

Ready to get started?Download WordPress

Forums

Get Children and Loop Through Each Child (5 posts)

  1. equaldesign
    Member
    Posted 4 years ago #

    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.

  2. equaldesign
    Member
    Posted 4 years ago #

    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 } ?>
  3. vjnoone
    Member
    Posted 4 years ago #

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

  4. anabelle
    Member
    Posted 3 years ago #

    How can you do this with categories?

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

  5. alchymyth
    The Sweeper & Moderator
    Posted 3 years ago #

    for child categories:

    <?php
    $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
        );
      query_posts($args); 
    
      if (have_posts()) : 
    
    echo $cat->name; //optional
    
        while(have_posts()) :
        the_post();
          /*here what you want to output*/
        endwhile; 
    
      endif; 
    
    endforeach;
    ?>

    http://codex.wordpress.org/Function_Reference/get_categories

Topic Closed

This topic has been closed to new replies.

About this Topic