Create you own Excerpt in WordPress


Sponsored Links




Sometimes we need to limit how many words will be displayed in our excerpt, If you have use WordPress 2.9.x you can use Easy Excerpt method, but if you want something different, you can create own excerpt and replace your original excerpt.

Custom Excerpt in WordPress

Custom Excerpt in WordPress

To create own excerpt in your WordPress, write down this code below in your functions.php

<?php add_filter('the_excerpt', 'my_excerpts');
function my_excerpts($content = false) {
			global $post;
			$mycontent = $post->post_excerpt;

			$mycontent = $post->post_content;
			$mycontent = strip_shortcodes($mycontent);
			$mycontent = str_replace(']]>', ']]&gt;', $mycontent);
			$mycontent = strip_tags($mycontent);
			$excerpt_length = 55;
			$words = explode(' ', $mycontent, $excerpt_length + 1);
			if(count($words) > $excerpt_length) :
				array_pop($words);
				array_push($words, '...');
				$mycontent = implode(' ', $words);
			endif;
			$mycontent = '<p>' . $mycontent . '</p>';
// Make sure to return the content
	return $mycontent;
}
?>

For some reason, if you want to keep original excerpt and only need this code to display on special place, you can remove the add_filter functions and then place this code in whatever you want it. Please note this code must be placed in The Loop.

<?php echo my_excerpts(); ?>

You have done modified original excerpt with your custom excerpt in WordPress. Original Idea, come from Justin Tadlock

Subscribe to Full RSS Feed

RSS FeedIf you thing this article useful, please consider subscribing to our RSS Feed or e-mail updates and stay updated with us. You can also follow @WPTricksNet on twitter for latest updates.

About WP Tricks

We are WordPress Family, we collected and writed useful tips and tricks for you WordPress. We share it for you...

Related Tips and Trick from Local Data and on The Net


25 Responses to Create you own Excerpt in WordPress

  1. Leslie Ardinger says:

    I am such a newbie to MySQL and have really enjoyed your tips and tricks!! I really need to study the code more!! I love the idea of your excerpt function but have received the error … Parse error: syntax error, unexpected ‘<', expecting T_VARIABLE or '$' … any thoughts? I am developing my own theme and am using WP 2.9.1. Hmmm…do you happen to have code for logging in users? your code is so clean and I prefer using code to plugins. One other I am looking for is the code for auto applying tags to comments and posts based upon check boxes at login time. I would store this info in DB. Thanks for any tips you have to offer! Leslie

    • WP Tricks says:

      Try to View Code then copy from that page and give me feedback, does the error still appear?

      • Leslie Ardinger says:

        This is the line 14 where my error occurs…$content = str_replace(‘]]>’, ‘]]>’, $content);

        When I view source, this is what I found…

        Parse error: syntax error, unexpected '<', expecting T_VARIABLE or '$' in C:\xampplite\htdocs\wordpress\wp-content\themes\somethinggood\functions.php on line 14

        Thank you for your help :)

        • WP Tricks says:

          I am really sorry, this code was auto rewriten by this plugin “SyntaxHighlighter Evolved” but I was fix it right now, please use my latest code that displayed on the post above.

          If you still got any problem, please let’s me know ;)

  2. Pingback: How to modify Custom excerpt length on WordPress # WordPress Tricks & Tips

  3. stupid says:

    stupid tutorial, won’t even work!

  4. we can create own excerpt and replace your original excerpt through this blog. I would store more information from this.

  5. Kasper says:

    you need to get rid of “” at the beginning and the end of the script before pasting it into functions.php.

    i guess that´s why a lot say it´s not working

  6. Paul Wolfe says:

    Is the best way to display excerpts to use a Plug In – or to use an inbuilt function of WordPress?

    TIA.

    paul

  7. deryk says:

    line 7 should be strip_shortcodes($content);

  8. Thomas says:

    There are a handful of WordPress Excerpt Plugins out there for those who are not comfortable editing the .php files in the backend.

    Here is one that I currently use…
    http://wordpress.org/extend/plugins/advanced-excerpt/

  9. Pascal says:

    Thanks for this brilliant solution. My theme didn’t accept the specialized excerpt_length, but with this it’s working fine. =)

    Best greetings

  10. Geergő says:

    could you write me how will the whole code look like, if you want to create a new excerpt tipe? (not replacing the custom one)

  11. Pingback: 20 More WordPress Code Snippets and Hacks

  12. Pingback: 20 More WordPress Code Snippets and Hacks | The best Tutorials

  13. Pingback: 20 More WordPress Code Snippets and Hacks | FlexLib

  14. Pingback: 20 More WordPress Code Snippets and Hacks | Wbcom Designs | VAPDI

  15. Pingback: 20 More WordPress Code Snippets and Hacks « Free UK Web Design

  16. Pingback: 20 More WordPress Code Snippets and Hacks | Preukson

  17. Pingback: 20 More WordPress Code Snippets and Hacks- rampantdesigntools.com

  18. Pingback: 20 More WordPress Code Snippets and Hacks | Programming Blog

  19. Pingback: 20 More WordPress Code Snippets and Hacks | Typography Blog

  20. I just miss programming. This is one sure of a professional work.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>