WordPress.org

Ready to get started?Download WordPress

Forums

limiting subpages. (5 posts)

  1. Mannyk29
    Member
    Posted 4 years ago #

    Hi,

    Immediatly after my Loop, in page.php, I am using the get_page() function to get the subpages of a current page. The code is as follows.

    <?php $pages = get_pages('child_of='.$post->ID.'&sort_column=post_date&sort_order=desc&parent='.$post->ID.'&heirarchical=1');
    $count = 0;
    $more = 1;
    foreach($pages as $page) {
    	$content = $page->post_content;
    	if(!$content)
    		continue;
    	$count++;
    	$content = apply_filters('the_content', $content);
    ?>
    
    <a href="<?php echo get_page_link($page->ID) ?>"><h1 class="title"> <?php echo $page->post_title ?> </h1></a> <?php echo $content ?>
    <?php
        }
    ?>

    The problem I am having is that all the descendents of the current page are displayed, not just the immediate children.

  2. vtxyzzy
    Member
    Posted 4 years ago #

    Try changing this:

    <?php $pages = get_pages('child_of='.$post->ID.'&sort_column=post_date&sort_order=desc&parent='.$post->ID.'&heirarchical=1');
    $count = 0;

    to this:

    <?php $pages = get_pages('parent=0&child_of='.$post->ID.'&sort_column=post_date&sort_order=desc&parent='.$post->ID.'&heirarchical=1');
    $count = 0;
  3. Mannyk29
    Member
    Posted 4 years ago #

    I made the change you suggested wirh no effect,

    However I did get the result I wanted by bruit force.

    <?php
    $more =1;
    $pages = get_pages('child_of='.$post->ID.'&sort_column=post_date&sort_order=desc&&heirarchical=1');
    $count = 0;
    foreach($pages as $page) {
    	$content = $page->post_content;
    	if(!$content)
    		continue;
    	$count++;
    	$content = apply_filters('the_content', $content);
    
    	// test to see if the child page is a first genneration child
    	if (get_parent_id($page->ID) == $post->ID) {
    ?>
    <a href="<?php echo get_page_link($page->ID) ?>"><h1 class="title"> <?php $page->post_title; ?> </h1></a> <?php echo $content; ?>
    <?php
    		}
        }
    ?>

    thanks again for your help

  4. vtxyzzy
    Member
    Posted 4 years ago #

    Sorry it didn't work. Please mark this topic 'Resolved'.

  5. sarthaksinghal
    Member
    Posted 4 years ago #

    The solution mentioned by "vtxyzzy" worked for me. Thanks a lot.

    ..Sarthak

Topic Closed

This topic has been closed to new replies.

About this Topic