57 Responses

  1. Dan Philibin
    Dan Philibin August 24, 2008 at 7:22 am | | Reply

    This is awesome; I’ve been looking for a solution to this for a long time. However, I really wish the_excerpt() had a link to the full article at the end. Since the ‘…’ can be customized here as well, why not add a link?

  2. Pangeran
    Pangeran August 24, 2008 at 7:27 am | | Reply

    So, that’s how filter works…

    Then, it means we can just modifying a theme with functions.php…
    Thanks for sharing this…

    But if we change the_content with this, how about plug in that add_action to the the_content() ?

    Will it still works?

  3. J Mehmett
    J Mehmett August 24, 2008 at 7:55 am | | Reply

    This works like a plugin, Nice trick! That’s why I hate plugins and do like to add everything from theme side.

    So, using filters and actions, child theme will work as a machine… Nice tip, friend.

  4. Darren Hoyt
    Darren Hoyt August 24, 2008 at 11:31 am | | Reply

    That’s a cool use of filters, Justin. These days I usually just include a file called ‘loop.php’ with my themes which uses a bunch of conditionals to determine content vs. excerpt depending on the page you’re on. I’m liking the idea of using filters now, though.

  5. Gustavo Ayres
    Gustavo Ayres August 24, 2008 at 12:42 pm | | Reply

    Hi Justin,

    This was exactly what I was looking for. I was trying to do something like that.
    The only problem I had is tha I use a plugin [can't remember the name] that I can add some BBCODE-like on my text and it makes a adsense banner appear. I put something like:

    [ad#banner-post]

    And it shows the banner.

    Using the code you put here, it doesn’t work anymore. Do you have any idea why it stopped work?

    Thanks in advance

  6. J Mehmett
    J Mehmett August 24, 2008 at 9:37 pm | | Reply

    I was thinking about adding some extra code in your above code, so I could make the links in the post appear in the the_excerpt(); too as they appear in the the_content();. Because, the_excerpt(); doesn’t show links in the post by default. I’ll give it a try.

    Of course the more we use functions.php the less we need a plugin.

  7. edoluz
    edoluz August 25, 2008 at 2:15 am | | Reply

    Thanks again Justin!! Great work!!!

  8. impNERD
    impNERD September 24, 2008 at 4:12 pm | | Reply

    This would be much better served as a plugin. You could easily set what pages this should effect as well.

  9. Nihar
    Nihar September 25, 2008 at 11:20 pm | | Reply

    Great tip. I will try to implement this.

    Thanks..

  10. baldo
    baldo September 29, 2008 at 4:46 am | | Reply

    Sorry guys but Im not a php guy. I follow the abouve procedure and put the code “// Add filter…” and “function my_excerpts…” to mu function.php under my present theme. But could not get it to work.

    The article post still displays the whole article. Need help.

  11. baldo
    baldo September 29, 2008 at 4:49 am | | Reply

    oh by the way, does it also filters even old posts? I wanted to reduce ALL articles on my blogs to show only a number of characters. Thanks

  12. Lucía
    Lucía October 30, 2008 at 7:47 am | | Reply

    Thanks!! This helped a lot, you made my day :D

  13. Sparrow
    Sparrow November 17, 2008 at 6:42 am | | Reply

    Hey Justin,

    This looks like the perfect solution to what I want to do. But you will have to forgive me as I am a newbie to the WordPress world. That being said, I am a little frustrated that WordPress’s own documentation and then here doesn’t say *where* to add this code or in what files..? Did I miss something?? As I said I am new, but it is like everybody assume that you know where these functions are!

    If you could advise I would quite grateful for the time it will save me sifting through files.

    Many thanks in advance!

  14. 11个新WordPress技巧 | Housne Space November 26, 2008 at 2:11 am |
  15. Barrett Golding
    Barrett Golding December 6, 2008 at 10:15 am | | Reply

    great script, Justin.
    i added a linked-to-post ellipsis ($postlink) at the end of the excerpt:

    Deleted by admin. Please use character entities when posting code.

    to get it w/in the auto-added tags of the “optional excerpt”,
    i added the link to your script here::
    $content = apply_filters('the_excerpt', $content . $postlink);

    then i also added it you your “if no excerpt” script here:
    $content = '' . $content . $postlink . '';

    not sure if that’s the best way to do job, but it does work in both cases:
    http://hearingvoices.com/news/2008/

  16. Barrett Golding
    Barrett Golding December 6, 2008 at 10:16 am | | Reply

    oh, forgot, i also removed your ellipsis here:
    array_push($words, '');

  17. Jauhari
    Jauhari December 20, 2008 at 7:00 pm | | Reply

    Hi Justin, Is it possible on the excerpt remove the Image only? and keep the other thing on the content just like the original?

  18. Brian Mecham
    Brian Mecham December 29, 2008 at 3:30 am | | Reply

    This doesn’t seem to work when your front page is a static page and not the blog page… I assume I’ll need to replace is_front_page with something else? but what?

  19. Brian Mecham
    Brian Mecham December 29, 2008 at 3:40 am | | Reply

    I guess I’ll reply to my own question since I figured it out… instead of is_front_page I put is_home – that’s in this part of the code in case you are wondering:
    // If is the home page, an archive, or search results
    if(is_home() || is_archive() || is_search()) :
    global $post;
    $content = $post->post_excerpt;

    This makes blog post excerpts when front page is static and blog page is a different page.

  20. Victor
    Victor February 4, 2009 at 3:40 pm | | Reply

    Very GOOD!

    Will try to use it!

    Thanks

  21. DrJ
    DrJ February 22, 2009 at 10:19 pm | | Reply

    Dude- you’ve saved my bacon. I’ve been struggling with the_content vs. the_excerpt as a n00b for the past few weeks, and your post made the “AHA!” bulb go off-

    Thanks very much!

  22. Mike Jump
    Mike Jump May 27, 2009 at 7:10 am | | Reply

    Thanks, I will implement this on my new developed themes. Mike

  23. freedimensional
    freedimensional June 6, 2009 at 5:45 pm | | Reply

    Hi Justin,

    Hope you are all good.

    I wonder if you could post a short tutorial on how to swap the_excerpt with the_content (yes, I mean to do the opposite!) in my theme?

    I’d like my readers to see everything without excerpts, but can’t suss out how to replace the functions.

    Tom

  24. Praz
    Praz June 15, 2009 at 9:01 am | | Reply

    Is it possible to remove the image from the_content() and display only the text in single.php. In which files should I make changes? Can someone help?

  25. PM
    PM July 28, 2009 at 9:02 pm | | Reply

    Thanks, this should help me with future wordpress projects.

  26. קולנוע ביתי
    קולנוע ביתי August 22, 2009 at 9:03 am | | Reply

    I am a newbie and before l I ‘ve read this post I have been working inefficiency.
    Due to your post I found the solution.
    I will definitely be implementing that !

  27. 24 Inch Bar Stools
    24 Inch Bar Stools August 22, 2009 at 12:13 pm | | Reply

    Very good tutorial Justin.
    Thank you.Helped me a lot.

  28. מתנה לחגים
    מתנה לחגים August 25, 2009 at 6:01 am | | Reply

    I’ve made some searching before upgradung my blog from blogger to WP.
    Your post made me the feeling that I am going to enjoy WP while there are such tools, themes, frameworks and solutions as this one.
    Thanks

  29. Ronald Nunez
    Ronald Nunez September 8, 2009 at 7:57 am | | Reply

    Hi Justin, Thanks a lot for posting this I have been looking around for this filter and just read from your post. This is really great and hope you are well. Keep on posting.

  30. Marvin
    Marvin September 10, 2009 at 10:47 am | | Reply

    Thanks for the tutorial.

    How can I modify this so that I can pass a specific word count value if I don’t want to use the default?

  31. HDD
    HDD September 26, 2009 at 2:27 am | | Reply

    This will help me a lot folks.

  32. Edds
    Edds November 6, 2009 at 10:50 am | | Reply

    Your post made me the feeling that I am going to enjoy WP while there are such tools, themes, frameworks and solutions as this one.
    I will definitely be implementing that !
    Thanks

  33. Andri
    Andri November 29, 2009 at 7:27 am | | Reply

    Uhm… what is the different between content and excerpt… sounds strange for me, sory newbie right here :)

  34. Phil
    Phil March 9, 2010 at 8:21 am | | Reply

    Thank you so much !

  35. baja hoodie
    baja hoodie March 12, 2010 at 5:29 am | | Reply

    I will use this on my new themes.

  36. Kim Lee
    Kim Lee October 7, 2010 at 3:45 am | | Reply

    Hi Justin,
    I am changing the_content with the_excerpt on almost every blog that I make. Naturally I have to repeat it on every new theme I upload and test. If I understood the benefit of your approach correctly, by making this adjustment, every theme that I upload to my blog will automatically display excerpt instead of content on the archives, front page and search page. Did I understand it correctly?

  37. Why I am not a Hybrid Core User November 17, 2010 at 7:13 pm |
  38. Swamykant
    Swamykant March 23, 2011 at 5:04 pm | | Reply

    Nice trick. But I want to the reverse way ?

    1. Jonathan Sampson
      Jonathan Sampson July 12, 2011 at 2:15 pm | | Reply

      Swamykant,

      Reversing it (delivering the_content in the place of the_excerpt) is even easier since there’s no danger of recursive calls:

      // Anytime my theme calls the_excerpt()`, call myCustFunct()
      add_action( 'the_excerpt', 'myCustFunc' );
      function myCustFunc ( $data ) {
        // Spit out the_content(), disregarding $data
        the_content();
      }

      Hope this helps.

  39. Keval
    Keval August 10, 2011 at 2:16 pm | | Reply

    Thank you so much! I recently had to reinstall my theme due to a coding mess error in the header.php, and when I reinstalled, I noticed my home page was out of whack because my previous coding had not been saved.

    I new integrating an excerpt was the only way, and this was the only set of instructions that worked perfectly for me. Thanks again!

  40. Sarbjit Singh
    Sarbjit Singh November 2, 2011 at 1:24 pm | | Reply

    You just saved me from editing my theme. I use child themes, so just needed to change one line of code. However, it is always better to do things with filters.

Leave a Reply

By submitting a comment here you grant this site a perpetual license to reproduce your words and name/Web site in attribution.

Please use your real name or a pseudonym (i.e., pen name, alias, nom de plume) when commenting. If you add your site name, company name, or something completely random, I'll likely change it to whatever I want.