WordPress.org

Ready to get started?Download WordPress

Forums

get_pages and limiting number of child pages displayed (6 posts)

  1. chefben
    Member
    Posted 4 years ago #

    I've got he following code working on a page, displaying all the children pages. However some pages have like 50 pages. So I'd like to restrict it to the last 10.

    <ul>
    <?php
    	$pages = get_pages('child_of='.$post->ID.'&sort_column=post_modified&sort_order=desc');
    	$count = 0;
    	foreach($pages as $page)
    	{ ?>
    		<a href="<?php echo get_page_link($page->ID) ?>"><?php echo $page->post_title ?></a><br />
    	<?php
    	}
    ?>
    </ul>

    However when I add &number=10 to the code below I get nil results.

    <ul>
    <?php
    	$pages = get_pages('child_of='.$post->ID.'&sort_column=post_modified&sort_order=desc&number=5');
    	$count = 0;
    	foreach($pages as $page)
    	{ ?>
    		<a href="<?php echo get_page_link($page->ID) ?>"><?php echo $page->post_title ?></a><br />
    	<?php
    	}
    ?>
    </ul>

    Anyone got any ideas ?

    Cheers

    Benjamin

  2. MichaelH
    Member
    Posted 4 years ago #

    <ul>
    <?php
    $count = 0;
    $pages = get_pages('child_of='.$post->ID.'&sort_column=post_modified&sort_order=desc');
      foreach($pages as $page) {
        $count++;
        if ( $count < 11 ) {  // only process 10 ?>
          <a href="<?php echo get_page_link($page->ID) ?>"><?php echo $page->post_title ?></a><br />
        <?php
        }
      }
    ?>
    </ul>
  3. chefben
    Member
    Posted 4 years ago #

    Michael,

    Thanks for this.

    Would you use similar code to get 10 Random ? Also struggling with this.

    Cheers

    Benjamin

  4. Zoinks! Graphics
    Member
    Posted 4 years ago #

    I too ran into the same issue with &number= not returning any results, using WP 2.9.2. So it doesn't work at all? Or just doesn't work with "child_of"?

    Either way, maybe someone should edit "number" in the get_pages parameters...

    http://codex.wordpress.org/Function_Reference/get_pages#Parameters

  5. radiofranky
    Member
    Posted 4 years ago #

    I'm using the following code to display "child". i was wondering is there a way not to display "grandchild"? I didn't see any option for it.

    thanks

    <?php
    $count = 0;
    $pages = get_pages('child_of=1119&sort_column=post_date&sort_order=desc');
      foreach($pages as $page) {
        $count++;
        if ( $count < 50) {  // only process 10 ?>
         <div class="main_post_listing">  <a href="<?php echo get_page_link($page->ID) ?>"><?php echo $page->post_title ?></a><br /></div>
        <?php
        }
      }
    ?>
  6. radiofranky
    Member
    Posted 4 years ago #

    i got it working

    <?php wp_list_pages('title_li=&depth=1&child_of=1119&sort_column=post_date&sort_order=desc&number=10'); ?>

Topic Closed

This topic has been closed to new replies.

About this Topic