2

Can you please help me on how can I display a list <li> of newly published child pages. I've been looking on google how to use display it using the wp_list_pages(); but can't find out there. I want to display it like in the "Latest Trainings" or something like that.

2
  • Child pages of what parent?
    – s_ha_dum
    Commented May 1, 2015 at 14:14
  • I'll like to display child pages of all parents I have
    – Kiko Lolo
    Commented May 1, 2015 at 14:18

3 Answers 3

1

This will get you the post data for all child pages:

$args = array(
    'post_type'         => 'page',
    'post_parent__not_in'       => array(0),                               
    'no_found_rows'     => true,
);
$child = new WP_Query($args);
var_dump(wp_list_pluck($child->posts,'post_title')); // debugging only

Then pass the IDs to wp_list_pages():

$args = array(
    'post_type'         => 'page',
    'post_parent__not_in'       => array(0),                               
    'no_found_rows'     => true,
);
$child = new WP_Query($args);
$ids = wp_list_pluck($child->posts,'ID');
$ids = implode($ids,',');
$args = array(
    'include'      => $ids,
);
wp_list_pages($args);

Or, even cleaner:

$args = array(
    'post_type'         => 'page',
    'post_parent__not_in'       => array(0),                               
    'no_found_rows'     => true,
    'fields' => 'ids'
);
$child = new WP_Query($args);
$args = array(
    'include'      => $child->posts,
);
wp_list_pages($args);

Please note that there is no error checking to that code.

I'll see if I can spot a clean way to do this with filters. Check back later.

5
0

Try using the following code to generate the child pages.

<?php
// Set up the objects needed
$my_wp_query = new WP_Query();

// Query all pages
$all_wp_pages = $my_wp_query->query(array('post_type' => 'page'));

$page_ids = get_all_page_ids();

$page_children = get_page_children( $page_ids, $all_wp_pages );

echo '<li>' . print_r( $page_children, true ) . '</li>';
?>
0

If you're really just looking to get a list of children with wp_list_pages(), then you should use the child_of parameter to do so. But I'm assuming you're actually want to restrict that list to a certain time frame, for this wp_list_pages(), which uses get_pages(), isn't really good equipped.

So I'm thinking you should use get_posts() - or even WP_Query directly -, where you can make use of »WP_Query: Date Parameters«. The drawback here - as a generic functionwp_list_posts() does not exist - is that you have to write the code to generate your list yourself. @s_ha_dum's answer shows how you can combine this, making the drawback almost disappear - just a little bit more code is needed.

3
  • but how can i implement this variable(only child pages) using that get_posts() and im working to only limit the list to show something like show="5"
    – Kiko Lolo
    Commented May 1, 2015 at 14:23
  • There are the parameters - see WP_Query documentation for details - post_parent and post_parent__in to show children, to regulate the number use posts_per_page. @KikoLolo Commented May 1, 2015 at 14:27
  • Take a closer look at s_ha_dum's answer, because it addresses the all children of all pages aspect and shows how to use it with wp_list_pages. @KikoLolo Commented May 1, 2015 at 14:36

Your Answer

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

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