gabesands
Member
Posted 9 months 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
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.
gabesands
Member
Posted 9 months ago #
like this?
$pages = get_pages('child_of='.$post->ID.'&sort_column=post_date&sort_order=desc&parent=.$post->ID');
gabesands
Member
Posted 9 months ago #
maybe a conditional statement might work?
No, like this:
$pages = get_pages('child_of='.$post->ID.'&sort_column=post_date&sort_order=desc&parent='.$post->ID);
gabesands
Member
Posted 9 months ago #
That worked flawlessly!!!!
Thank you so much.
P.S. I've added extra keywords so other noobs like me can find this post.
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... ;)
gabesands
Member
Posted 7 months 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!
gabesands
Member
Posted 7 months 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>'
alexmglover
Member
Posted 2 weeks 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.