Reputation: 116
I am pretty new to wordpress, and wondering if someone could shed some light on this code. I am trying to list all sub pages on their parent page, here is the code with some html stripped out:
<?php
$mypages = get_pages( array( 'child_of' => $post->ID ) );
foreach( $mypages as $page ) {
$content = $page->post_content;
if ( ! $content ) // Check for empty page
continue;
$content = apply_filters( 'the_content', $content );
?>
<p style="color: white; text-transform: uppercase;"><?php echo $page->post_title; ?></p>
<?php
}
?>
The code works, and correct sub pages are displayed - but not all of them. The 7 oldest posts are showing, but none of the newest pages that I created this week. I have checked and double checked that all new and old pages are the same in every way - same template, same parent page, same creator, same order number, and all published. Anyone have an idea of what I could be doing wrong?
Upvotes: 5
Views: 3544
Reputation: 6080
I think you need additional argument as well to get result you are looking for.
$args = array(
'child_of' => $post->ID,
'parent ' => $post->ID,
'hierarchical' => 0,
'sort_column' => 'menu_order',
'sort_order' => 'asc'
);
$mypages = get_pages( $args );
You can check wordpress Doc for its argument and return output.
NOTE If you want to sort pages by date then you can change 'sort_column' => 'menu_order',
with 'sort_column' => 'post_date',
.
There is other method as well to achieve the same and I prefer below way.
<?php
$args = array(
'post_type' => 'page',
'posts_per_page' => -1,
'post_parent' => $post->ID,
'order' => 'ASC',
'orderby' => 'menu_order'
);
$mypages = new WP_Query( $args );
if ( $mypages->have_posts() ) : ?>
<?php while ( $mypages->have_posts() ) : $mypages->the_post(); ?>
<p style="color: white; text-transform: uppercase;"><?php the_title(); ?></p>
<?php endwhile; ?>
<?php endif; wp_reset_query(); ?>
You can also use wp_list_pages to render direct HTML.
Upvotes: 1
Reputation: 116
I have fixed the issue, but I actually have no idea why this works. Any comments about why this did the trick would be awesome.
From my original code, I removed the "if ( ! $content )" section, and they all show up - even though all pages have the same amount of content.
So, in the end, my code reads:
<?php
$mypages = get_pages( array( 'child_of' => $post->ID ) );
foreach( $mypages as $page ) {
$content = $page->post_content;
$content = apply_filters( 'the_content', $content );
?>
<p style="color: white; text-transform: uppercase;"><?php echo $page->post_title; ?></p>
<?php
}
?>
I never had to change the get_pages function parameters at all.
Upvotes: 0
Reputation: 3614
Try below code:
$args = array('child_of' => $post->ID,'sort_order' => 'desc',
'sort_column' => 'ID',
);
Upvotes: 1
Reputation: 1189
Try using parent instead of child_of and setting hierarchical to false. According to the docs it appears the default hierarchical value of true can can affect the results.
<?php
$mypages = get_pages( array( 'parent' => $post->ID, 'hierarchical' => 0 ) );
?>
Reference: https://codex.wordpress.org/Function_Reference/get_pages
Upvotes: 0