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.
-
Child pages of what parent?– s_ha_dumCommented May 1, 2015 at 14:14
-
I'll like to display child pages of all parents I have– Kiko LoloCommented May 1, 2015 at 14:18
3 Answers
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.
-
-
@KikoLolo One problem per question, if this is solved, then accept what solved it for you. You might find useful information here »What should I do when someone answers my question?« and generally at the help center. Ask a new question concerning your new problem. Commented May 1, 2015 at 15:29
-
-
1I am pretty sure I ran that code earlier and it worked. I can't re-test it right now.– s_ha_dumCommented May 1, 2015 at 15:42
-
1See the following for a trivial shortcode sample: wordpress.stackexchange.com/a/98312/21376– s_ha_dumCommented May 1, 2015 at 15:46
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>';
?>
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 function @s_ha_dum's answer shows how you can combine this, making the drawback almost disappear - just a little bit more code is needed.wp_list_posts()
does not exist - is that you have to write the code to generate your list yourself.
-
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" Commented May 1, 2015 at 14:23
-
There are the parameters - see
WP_Query
documentation for details -post_parent
andpost_parent__in
to show children, to regulate the number useposts_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