Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am using the following code to display child page content on it's parent page. The code is working great, but it currently only shows the latest two child page posts, is there any way to display all of them and then add pagination to navigate through them?

<?php
    $pages = get_pages('child_of='.$post->ID.'&sort_column=post_date&sort_order=desc');
    $count = 0;
    foreach($pages as $page) {
        $content = $page->post_content;
        if($count >= 2)
            break;
        $count++;
        $content = apply_filters('the_content', $content);
        echo "<h2>".$page->post_title."</h2>";
        echo "<p>".$content."</p>";
    }
?>

Thanks,
Josh

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

After some searching I was able to find this post: http://wordpress.org/support/topic/add-pagination-to-list-of-child-pages

My final code looks like this:

<?php
    $ids = array();
    $pages = get_pages("child_of=".$post->ID);
    if ($pages) {
        foreach ($pages as $page) {
            $ids[] = $page->ID;
        }
    }
    $paged = (get_query_var("paged")) ? get_query_var("paged") : 1;
    $args = array(
        "paged" => $paged,
        "post__in" => $ids,
        "posts_per_page" => 2,
        "post_type" => "page"
    );
    query_posts($args);
    if (have_posts()) : while (have_posts()) : the_post(); 
?>
    <h2><?php the_title(); ?></h2>
    <p><?php the_content(); ?></p>
<?php endwhile; else: ?>
    <h2>Oh No!!</h2>
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
<?php echo get_next_posts_link(); ?>
<?php echo get_previous_posts_link(); ?>
<?php wp_reset_query(); ?>

I really like this code because it pulls the content using a loop, making it easier to pull the things you want from the page (as well as creating a custom query)...I edited a few lines from the link I found and tried to simplify it as much as possible.

I hope this helps someone else out there, worked like a champ for me!

Note: I wanted to display just the child pages and no grand child pages, so if you'd also like to do that add "parent=".$post->ID to line 3 in the code above. *The line will look like: $pages = get_pages("parent=".$post->ID."&child_of=".$post->ID);

Thanks,
Josh

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.