anthonynoel
Member
Posted 2 years ago #
Hello,
I'm using the following code in a table cell to show a list of page excerpts
<?php $pages = get_pages('child_of='.$post->ID.'&sort_column=post_date&sort_order=asc');
$count = 0;
foreach($pages as $page)
{
$excerpt = $page->post_excerpt;
if(!$excerpt)
continue;
$excerpt = apply_filters('the_excerpt', $excerpt);
?>
<a href="<?php echo get_page_link($page->ID) ?>"><?php echo $excerpt ?></a>
<?php
}
?>
but rather than just show the subpages of the current page, it will show all subpages.
At the moment it works and only shows direct descendants if I hard code in the page ID, but I'd like to make this a template I can implement onto other pages without having to edit the code each time.
Any ideas?
Cheers
Anthony
Re you using that code within The Loop?
anthonynoel
Member
Posted 2 years ago #
Yes. At least I think so. It's not in sidebars, the header or posts.
It's on a static page (indicated by*), under this structure:
Home
Services
Portfolio
Non-fiction books
––––Series Title 1*
––––––––Title 1†
––––––––Title 2†
––––––––Title 3†
––––Series Title 2*
––––––––Title 1†
––––––––Title 2†
Educational books
––––Series Title 1*
––––––––Title 1†
––––––––Title 2†
About
Imprint
When I use $Post->ID to identify the page as the parent page of the children, it retrieves all grandchild pages (indicated by †) rather than just the direct descendants of that page.
If you're going to use $post->ID, you do need to ensure that the code is within the Loop rather than just somewhere in a template file. Outside the Loop, you're going to get unpredictable results.
anthonynoel
Member
Posted 2 years ago #
Okay. I think I need to understand the difference between posts and pages better. Until then, I will just hardcode this since it is only a few pages at the moment.
Thanks
Anthony
anthonynoel
Member
Posted 2 years ago #
Unless, of course, there's a better way to call a page ID outside of the loop?
You could try $wp_query->get_queried_object_id(); outside the loop. Not sure how reliable that is, but should work outside the loop.
anthonynoel
Member
Posted 2 years ago #
'.$page->ID.' instead of $post seems to do the same thing.
Outside the loop i'd look at $wp_query, it holds the main queried object and associated data, hence the suggestion above.. :)
anthonynoel
Member
Posted 2 years ago #
I believe our posts crossed.. thank you, I'll check that out.
A.