Get excerpt from parent page
-
Hi.
How can I get the excerpt from a parent page, when viewing a sub page?
I have tried with:
$pages = get_pages('parent='.$post->post_parent); foreach($pages as $page) { echo $page->post_excerpt; }Thanks
Vayu
-
If you only need to grab info on the parent, then you don’t need to get_pages, you’re grabbing all pages with the same parent in your above code.
Try this instead, assuming you’re doing this in the loop in page.php or similar.
<?php echo get_the_excerpt( $post->parent ); ?>Since the parent ID is available in $post->parent, just call what you need…
If you want the title, again, just like above…
<?php echo get_the_title( $post->parent ); ?>Let me know if that helps… π
Thanks t31os_, this is a good, but it doesnt quite work for some reason.
get_the _title works fine like you say, but get_the_excerpt doesnt. It just gets the excerpt of the current page and not the excerpt of the parent.
Any clue?
Have you tried using
<?php echo get_the_excerpt( $post->parent ); ?>?What about trying
<?php echo wp_trim_excerpt( get_the_content( $post->parent ) ); ?>?Hi Esmi.
Yes, I did try <?php echo get_the_excerpt( $post->parent ); ?> and it just gives me an excerpt of the current page, not the parent page.
The same happens when I use <?php echo wp_trim_excerpt( get_the_content( $post->parent ) ); ?>.
Im starting to think that there may be something wrong here.
Thanks for your help though.
Are you replacing what you posted before with what’s suggested? or are you adding to it?…
What’s been posted will totally replace what you supplied in your opening post.
Yes, I have replaced what I initially wrote. It is deleted.
This is basically all I have in the page.php:
<?php get_header(); ?> <?php if(have_posts()) : while(have_posts()) : the_post(); ?> <div id="content"> <h1>My title</h1> <?php echo get_the_excerpt( $post->parent ); ?> </div> <?php endwhile; endif; ?> <?php get_footer(); ?>It just gives me the excerpt of the current page and not the parent page. Strange eh!
Sorry i see the mistake now..
This.
<?php echo get_the_excerpt( $post->parent ); ?>
Should be.
<?php echo get_the_excerpt( $post->post_parent ); ?>However, you can’t use get_the_content or get_the_excerpt, etc.. inside the post loop, which is where you get your parent ID from… Or at least it won’t accept ID’s for me when i tried… it just ignores what it’s given and uses the current post’s (or page’s) ID instead, regardless of what’s fed to it..
The alternative is to grab the data using get_pages with an include statement and use the wp_trim_excerpt filter on the result..
<?php $ppage = get_pages( 'include='.$post->post_parent ); // Only grab the parent echo wp_trim_excerpt( $ppage[0]->post_content ); // Run excerpt filter on the only item in the array unset($ppage); // Unset all the data in $ppage because it's no longer needed ?>Hi t31os_. I really appreciate all your time and effort.
I knew about this mistake all the time and have corrected it in all my attempts to figure this out. But it didnt make any difference as you also mention.
Wrong: $post->parent
Correct: $post->post_parentI did try with your get_pages code, but I still get the same result. I only get the excerpt for the current page.
At this moment I am starting to give up on this…
Thanks again.
Vayu
I tested the code above before i posted it, it most certainly does work.
Like i said, it won’t work with post_parent using get_the_excerpt etc… so the above was an alternateive to work around that problem.
I’ve just tried the code again to be sure (placed just after the
endwhilefrom the regular loop), and again it does work.. or at least it’s grabbing content for the parent page, whether the excerpt filter works i don’t know because my test install only has very small amounts of content in the posts and pages, but it’s certainly grabbing the parent’s content and not the current page’s.Is there away to do this, but displaying the excerpt of the child on the parent page?
I want the parent page to display summaries of sub-pages.
I found a solution.
snilesh.com/resources/wordpress/wordpress-hacks-and-tricks/wordpress-show-title-and-excerpt-of-child-pages-on-parent-page/
The topic ‘Get excerpt from parent page’ is closed to new replies.