Excerpts for Pages in WordPress 3.0

Posted on

By Michael Fields

This post is a bit nostalgic as it references the first topic that I ever wrote about WordPress on a blog. A little over two years ago, I wrote a post called How to Activate Excerpts for Pages in WordPress Admin Panel which explains the work that I had done while migrating my custom theme from WordPress 2.3.3 to WordPress 2.5.

The post was updated when WordPress 2.9 came around because someone commented that the cool drag and drop feature wasn’t working with the 2.5 code. This morning, I updated the page again for WordPress 3.0 – probably for the last time ever!

The WordPress developers have been adding really cool functionality to the core for the soon-to-be-released 3.0. one of the features that I’m really excited about is custom post types. I was looking through the core this morning when I realized that some of the functions behind this feature would allow users to add excerpts to pages with a single line of code:

add_post_type_support( 'page', 'excerpt' );

Drop this line in your active theme’s functions.php file and you’re good to go… probably forever!

From 2.3 though 2.9 I have came up with 3 different methods for enabling excerpts for pages. Why? You ask. Easy answer: WordPress changed the html that displayed the excerpt’s meta box in the administration panels. The example above contains no html – It’s even small enough to post on Twitter – It will always use whatever html WordPress needs to function properly eliminating the need to update every other version.

Now that’s progress!

38 Comments Comments are closed

  1. Lance July 27, 2010 at 1:04 pm

    Thanks, this was very helpful! I couldn’t understand where the excerpt field had gone. Following your instructions I had it back in 20 seconds.

  2. Thomas August 2, 2010 at 6:34 am

    Thanks, that works also for PHP illiterates like me… :-)

  3. Valentin September 25, 2010 at 10:12 am

    Awesome snippet, I’d been using an extremely naggy plugin to add excerpts to pages, and adding one line to the functions.php is so much easier, thanks for sharing!

  4. Michael Fields September 25, 2010 at 11:00 am

    No problem! I agree, nothing beats one line of code ;)

  5. ali October 21, 2010 at 3:42 pm

    This is great and just what I was looking for. Any idea’s how I could use this to display the page excerpt in my sidebar?

    Many thanks

  6. anniebme November 24, 2010 at 12:31 am

    Thanks! This makes life a lot nicer. ^_______^

  7. jenna December 5, 2010 at 11:16 pm

    Thanks for posting this! I have a page on one site that has to be edited weekly. Saving it was making the excerpt disappear. I use excerpts in the theme, so every week I was logging into phpMyAdmin to put the excerpt back on that page. You saved me a lot of work.

  8. Michael Fields December 5, 2010 at 11:20 pm

    That sounds like a nightmare… yikes! Really happy you do not have to do that anymore :)

  9. Aiwe February 27, 2011 at 7:54 pm

    Thank you for this tip. I helped me to get rid of plugins. With this I made great use of page excerpts for featured pages.

  10. Michael Fields February 28, 2011 at 8:48 am

    Glad you found it useful. I agree, it’s much better than using a plugin to accomplish the same task as they will most likely need maintenance in the future.

  11. Rafael March 22, 2011 at 7:03 pm

    Michael, I respectfully request advice to get this working.

    I’ve spent 25 minutes researching this topic. I found your fix above and edited functions.php for Weaver 2.0 theme in WordPress 3.1. I have logged out/in of the site and cleared cache. I have looked at available screen options and see nothing about Excerpts. I don’t see the excerpts at the bottom of the Edit Page screen.

    Is there some additional edit I need to do to the theme, or other configuration somewhere, to actually show the excerpt in the Edit Page screen?

    I have activated the ExcerptEditor Plugin which was my old workaround. But it seems messed up in WordPress 3.1 and I can remove it once I get your fix working.

    Sorry if this is a dumb question. Thanks.

  12. Michael Fields March 25, 2011 at 12:24 am

    Not sure as to why this is not working for you. You’ve performed all of the tests that I would suggest already. As a last resort please try the following code. Could be a theme bug?

    
    function _mychildtheme_setup() {
    	add_post_type_support( 'page', 'excerpt' );
    }
    add_action( 'after_setup_theme', '_mychildtheme_setup', 11 );
    
  13. Saurabh Shukla April 19, 2011 at 6:58 am

    Hi,

    This should be obvious in wordpress codex, but isn’t. I used to always wonder why when pages are technically the same as posts, can’t we access such features.

    What I wonder about is-

    Can this function also add support for featured images, tags, etc for pages in similar ways?

  14. Michael Fields April 19, 2011 at 8:41 am

    Saurabh,

    Sorry to hear that this is not as obvious as it should be in the codex. Please post links to the pages that you think this information should be on and I will do my best to edit them.

    TIP: Anyone can edit the codex. If you find that something is missing, please add it!

    FYI: Pages are NOT technically the same as posts. They are actually very different in many ways.

    Can this function also add support for featured images, tags, etc for pages in similar ways?

    Absolutely! Personally, I would never use tags on pages or any post_type other than tags. Just check out the taxonomy’s full name “post_tag” and you should see why. This taxonomy is intended to be used for “posts” and I never use it for anything else.

    Best wishes,
    -Mike

  15. jamie April 19, 2011 at 5:10 pm

    I don’t understand why you would ever need an excerpt on a page?

  16. Michael Fields April 19, 2011 at 6:22 pm

    Pages show up in many different archive pages including search. Many people want control over the excerpt. I’ve custom things with page excerpts in the past … excerpts ARE a part of the posts table. So I always activate them.

  17. gearu May 7, 2011 at 3:27 am

    if this does not show up immediatley for you, then you might need to click on the ‘screen options’ (little tab near the top right of the page when you are viewing a page. there is a checkbox for Excerpts – the field only shows up when this is clicked (and you have added the line in the functions file)

  18. Tim September 27, 2011 at 4:59 pm

    Dear Mike,

    Tim here. I have added the appropriate code from above as suggested, Have add the excerpt from a page. Published the page.

    But what I go to the page in question it doesn’t show the excerpt.

    If you go to: http://members,timothycaron.com
    and click on the center tab in the top menu you will notice that when the page loads it shows the full content of each page not the excerpt.

    Can you assist.
    Thanks in advance
    Tim

  19. Michael Fields September 27, 2011 at 5:02 pm

    Hi Tim! You’ll have to modify your theme. You’ll need to use the_excerpt() in the file that you want the page excerpt to display. I generally use this in search.php and page.php.

  20. Tim September 27, 2011 at 7:20 pm

    Dear Michael,

    To be honest I wasn’t expecting a response on an old post. Thank you.

    I did as you requested, and replaced $content with $the_excerpt and now I have no content at all under the titles, but have the full page of the parent one.

    Really strange.
    Tim

  21. Michael Fields September 27, 2011 at 7:23 pm

    No problem. I’m not sure what is going on in your theme … might be something custom? Please change $the_excerpt back to $content and then add:

    <?php the_excerpt(); ?>

    wherever you would like the excerpt displayed. It is a function, not a variable.

  22. Tim September 27, 2011 at 7:48 pm

    Dear Mike,

    Tim here. You are correct I am designing a custom page template for wordpress. I have added code below the loop to bring in the child page of the parent, and it’s the excerpt of the child page that I want to bring in.

    Anyway you can help
    I’ll attache the code below.
    Thanks
    Tim

  23. Tim September 27, 2011 at 7:49 pm

    $post->ID, ‘sort_column’ => ‘post_date’, ‘sort_order’ => ‘desc’ ) );

    foreach( $mypages as $page ) {

    $content = $page->post_content;

    if ( ! $content ) // Check for empty page

    continue;

    $content = apply_filters( ‘child_of’, $content );

    ?>

    <a href="ID ); ?>”>post_title; ?>

  24. Saurabh Shukla September 27, 2011 at 7:57 pm

    Hi Tim,

    I couldn’t help but butt in. I have used this solution in a couple of sites.

    This is a problem that is independent of Michael’s solution.

    here’s the code:

    <article id="post-" >

    <a href="" title="Continue reading post_title; ?>">post_title; ?>

    post_excerpt; ?><a href="" title="Continue reading post_title; ?>" class="rmore" rel="bookmark">Read More

    <?php
    }
    if($is_empty) { echo 'Eh! Nothing in here?'; } wp_reset_query(); ?>

  25. Saurabh Shukla September 27, 2011 at 7:59 pm

    That got royally truncated:

    One more attempt:
    <div id=”featured” class=”clearfix”>
    <?php
    $pg_ids= array(6,8,10,12);
    foreach ($pg_ids as $pg_id){
    $post=get_post($pg_id);
    ?>
    <article id=”post-<?php the_ID(); ?>” <?php post_class(); ?>>

    <header><h2><a href=”<?php the_permalink(); ?>” title=”Continue reading <?php echo $post->post_title; ?>”><?php echo $post->post_title; ?></a></h2></header>
    <?php
    if ( has_post_thumbnail() ) {
    the_post_thumbnail(array(226,226));
    }
    ?>

    <p><?php echo $post->post_excerpt; ?><a href=”<?php the_permalink(); ?>” title=”Continue reading <?php echo $post->post_title; ?>” class=”rmore” rel=”bookmark”>Read More</a></p>
    </article>

    <?php
    }
    if($is_empty) { echo ‘<p align=”center” style=”padding:5px; margin:0;”>Eh! Nothing in here?</p>’; } wp_reset_query(); ?>
    </div>
    <div id=”main”>

    <?php get_template_part( ‘loop’, ‘index’ ); ?>
    </div>

  26. Tim September 27, 2011 at 8:09 pm

    Dear Saurabh Shukla,

    let me try this code again so you can see what I’m doing.

    $post->ID, ‘sort_column’ => ‘post_date’, ‘sort_order’ => ‘desc’ ) );
    foreach( $mypages as $page ) {
    $content = $page->post_content;
    if ( ! $content ) // Check for empty page
    continue;
    $content = apply_filters( ‘child_of’, $content );
    ?>

    <a href="ID ); ?>”>post_title; ?>

    This portion of code brings in all the child pages of the parent page they are listed under.

    It can’t be a list of page ID’s as you have. It’s generated dynamically.

    Tim

  27. Saurabh Shukla September 27, 2011 at 8:16 pm

    use post_excerpt instead of post_content.
    sorry for my earlier reply. i realise, it was pretty useless

  28. Saurabh Shukla September 27, 2011 at 8:22 pm

    <?php
    $p_id = $post->ID;
    //this gets the current page
    $pages= get_posts(‘child_of’ => $p_id);
    //this gets the child pages of current page in an object
    //now you can run a for loop on each $pages as $page
    and output $page->post_excerpt;
    ?>

  29. Saurabh Shukla September 27, 2011 at 8:34 pm

    Okay, that might not work, as well, I think get_posts deosn’t support the child_of argument.

    You’d have to go roundabout.

    first get the current page id the way above. then use this to get child pages

    <?php
    $p_id = $post->ID;
    //this gets the current page
    $pages= get_pages(array(‘child_of’ => $p_id));
    //this gets the child pages of current page in an object
    for each($pages as $pagg){
    $childpg=$get_post($pagg->id);
    //we have to again use get-post because get_page will ot get you the excerpt. so, yeah, roundabout is the way!
    echo $childpg->post_excerpt;
    }
    ?>

    This must work

  30. Saurabh Shukla September 27, 2011 at 8:36 pm

    I’ve tried and tested it just now. It works! Let me know how it goes with you.

  31. Saurabh Shukla September 27, 2011 at 8:40 pm

    I meant in the last to last comment that get_pages doesn’t return the excerpt. get_posts doesn’t support child_of.

    So, you use get_pages to get child pages. Loop through, get the id and use it with either get_post or get_page to get the excerpt.

  32. John October 22, 2011 at 10:09 pm

    Brilliantly simple. Thanks for sharing this useful tidbit!

  33. Matthew Fries November 12, 2011 at 1:08 am

    This worked beautifully as a way feature new pages on the site. Thanks for finding this!

  34. JK November 28, 2011 at 1:06 am

    Works perfectly. Thanks for the great tip.

  35. Rachel Nabors December 21, 2011 at 4:25 pm

    I always end up on your blog posts when googling for arcane WP tweakery. All roads lead to Rome.

  36. Jay February 2, 2012 at 3:24 am

    I had a plugin that pulled from the excerpt, but no excerpt field! I added your one-liner, it worked.
    You, sir, are my hero of the day!

Fork me on GitHub