0

I want to output all sub-pages of a particular page in a neat ul li list. Including: featured image, title, and first 50 characters of page content.

I'm very new to Wordpress, I've tried reading through the codex and I seem to have found something but I cannot get it to work.

First, I have created a new template of "list page" (listpage.php) so that I can choose pages where I wish them to list out sub pages.

I still need the content of that page to display, and the ul li list to output below, essentially so the client can add some intro text.

Here is the code I have from the codex

<?php get_page_children( $page_id, $pages ) ?>

I tried to change the parameters to meet my requirements, the page ID is page 6 (you can see it here)

I cannot work out what the $pages parameter is meant to be, but looking at other examples on the Codex page it seems that $all_wp_pages can be used.

So I tried this:

<?php get_page_children( $6, $all_wp_pages ) ?>

And got an error:

Parse error: syntax error, unexpected T_LNUMBER, expecting T_VARIABLE or '$' in /mnt/vol2/home/b/a/barkin10/public_html/wordpress/wp-content/themes/Yasmin/listpage.php on line 27

So I really don't know what I'm supposed to be doing :(

Also...

Even if this did work, because I have to put the page ID it has occured to me that this listpage template will only be useable on the page that the ID corresponds to. I want to be able to give any page a template of list page and be able to output the children (featured image, page title and first 50 chars) but no idea how.

3
  • For syntax error, you need to remove dollar sign from first parameter. for ex. change $6 to 6. Jun 17, 2013 at 15:45
  • Thanks, I'm new to PHP. Removed but still don't get anything output (though at least no error now).
    – Francesca
    Jun 17, 2013 at 15:47
  • It's alright, this was just to show you where you are wrong, so you can improve yourself in future. Bytheway i have added a full solution bellow for your problem. Jun 17, 2013 at 16:07

2 Answers 2

1

You can use for example very basic WP Query (or get_pages or what ever. this is just one example) (http://codex.wordpress.org/Class_Reference/WP_Query)

// The Query
$currentPageId = $post->ID; // get current page id inside loop
$args = array(
    'post_parent' => $currentPageId,
    'post_type' => 'page',
    'posts_per_page' => -1
    );
$query = new WP_Query( $args );

// The Loop
if ( $query->have_posts() ) {
    echo '<ul>';
    while ( $query->have_posts() ) {
        $query->the_post();
        echo '<li>' . get_the_title() . '</li>'; // this one outputs only title
    }
    echo '</ul>';
} else {
    // no posts found
}

/* Restore original Post Data */
wp_reset_postdata();

And there you have a list with titles for all the child pages of current page.

3
0

Here is another way you can get it.

$args = array(
'posts_per_page'  => 5,
'offset'          => 0,
//'category'        => '',
'orderby'         => 'post_date',
'order'           => 'DESC',
//'include'         => '',
//'exclude'         => '',
//'meta_key'        => '',
//'meta_value'      => '',
'post_type'       => 'page',
//'post_mime_type'  => '',
'post_parent'     => '5', // Enter your post parent here
'post_status'     => 'publish',
'suppress_filters' => true 
  );

 $all_posts=get_posts($args);
 echo '<ul>';//Your ul section
 foreach ($all_posts as $post) : setup_postdata( $post ); 
echo '<li>';// your li section
the_title();
    the_excerpt();
    the_post_thumbnail(); // You can use this if you're using the built-in feature image facility
  echo '</li>';
  endforeach;
  echo '</ul>';
   wp_reset_query();

Try this now.

2
  • According to the Codex, new WP_Query is the preferred method for creating separate loops: codex.wordpress.org/Template_Tags/get_posts Jun 17, 2013 at 17:21
  • As i have mentioned in starting of my post that, this is a another way to do this. Its on you to use it either by wpquery or by get_post(). Jun 18, 2013 at 16:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.