Forums

child_of gets all subpages (11 posts)

  1. 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

  2. esmi
    Theme Diva & Forum Moderator
    Posted 2 years ago #

    Re you using that code within The Loop?

  3. 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.

  4. esmi
    Theme Diva & Forum Moderator
    Posted 2 years ago #

    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.

  5. 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

  6. anthonynoel
    Member
    Posted 2 years ago #

    Unless, of course, there's a better way to call a page ID outside of the loop?

  7. Mark / t31os
    Moderator
    Posted 2 years ago #

    You could try $wp_query->get_queried_object_id(); outside the loop. Not sure how reliable that is, but should work outside the loop.

  8. anthonynoel
    Member
    Posted 2 years ago #

    '.$page->ID.' instead of $post seems to do the same thing.

  9. Mark / t31os
    Moderator
    Posted 2 years ago #

    Outside the loop i'd look at $wp_query, it holds the main queried object and associated data, hence the suggestion above.. :)

  10. anthonynoel
    Member
    Posted 2 years ago #

    I believe our posts crossed.. thank you, I'll check that out.

    A.

  11. Mark / t31os
    Moderator
    Posted 2 years ago #

    No problem.

    Link for related information.
    http://codex.wordpress.org/Function_Reference/WP_Query
    http://codex.wordpress.org/Function_Reference/WP_Query#Methods

    Should of posted that before.. :)

Topic Closed

This topic has been closed to new replies.

About this Topic