WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] page parent showing content of page child only and not page grandchild (10 posts)

  1. gabesands
    Member
    Posted 5 years ago #

    Hello everybody,

    I searched all over the WordPress forums and the internet, but I haven't been able to find any answers regarding only showing content of direct page child without showing content of page grandchildren on a page parent.

    I tried the code that is in the wordpress function reference page for get_pages and tweaking it, but I couldn't get it to do what I wanted.

    <?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(!$content)
                continue;
            if($count >= 2)
                break;
            $count++;
    //$content = apply_filters('the_content', $content);
        ?>
            <div class="entry"><a href="<?php echo get_page_link($page->ID) ?>"><h2><?php echo $page->post_title ?></h2></a><?php echo $content ?></div>
        <?php
        }
    ?>

    Thanks for the help,

    Gabe

  2. MichaelH
    Member
    Posted 5 years ago #

    Hmm just learned something new...

    Try adding .'&parent='.$post->ID to your parameter list of get_pages.

    I just added the description of that parameter to Function_Reference/get_pages so let me know how that works for you.

  3. gabesands
    Member
    Posted 5 years ago #

    like this?

    $pages = get_pages('child_of='.$post->ID.'&sort_column=post_date&sort_order=desc&parent=.$post->ID');

  4. gabesands
    Member
    Posted 5 years ago #

    maybe a conditional statement might work?

  5. MichaelH
    Member
    Posted 5 years ago #

    No, like this:
    $pages = get_pages('child_of='.$post->ID.'&sort_column=post_date&sort_order=desc&parent='.$post->ID);

  6. gabesands
    Member
    Posted 5 years ago #

    That worked flawlessly!!!!

    Thank you so much.

    P.S. I've added extra keywords so other noobs like me can find this post.

  7. Edde Beket
    Member
    Posted 5 years ago #

    Thanks! Just what I needed!

    @MichaelH: you might want to include the alternative $pages = get_pages('child_of='.$post->ID.'&sort_column=post_date&sort_order=desc&parent='.$post->ID); for child pages only - NOT including any grandchild pages - to the Function_Reference/get_pages Maybe just as a remark? It's a very nice addition.

    Using WordPress as a corporate (read: page and not post oriented) CMS is getting there - not that it stopped me before... ;)

  8. gabesands
    Member
    Posted 5 years ago #

    One more thing,

    How would I also fetch a custom field value with that call?

    Here is the code I used:

    <?php $pages = get_pages('child_of='.$post->ID.'&sort_column=post_date&sort_order=desc&parent='.$post->ID);
    $count = 0;
    foreach($pages as $page)
    {
    	$content = $page->post_content;
    ?>
    <div class="item">
    	<a href="<?php echo get_page_link($page->ID) ?>"><?php echo $page->post_title ?></a>
    	<?php echo $content ?>
    
    </div>
    <?php } ?>

    It displays the content , but I also want to be able to get a specific custom field value.

    Thanks!

  9. gabesands
    Member
    Posted 5 years ago #

    I solved it thanks to this post http://wordpress.org/support/topic/218997

    '<?php $pages = get_pages('child_of='.$post->ID.'&sort_column=post_date&sort_order=desc&parent='.$post->ID); $count = 0; foreach($pages as $page) { $content = $page->post_content;$price = get_post_meta($page->ID, 'price', $single = true); ?>
    <div class="item">ID) ?>"><?php echo $page->post_title ?><p><?php echo $content ?></p><?php echo $price ?></div>
    <?php } ?>
    </div>'

  10. alexmglover
    Member
    Posted 4 years ago #

    Using get_pages is there any way to just grab the post ID of the child as it loops through, like the code above loops.

Topic Closed

This topic has been closed to new replies.

About this Topic