Hot answers tagged

117

COMPLETE GUIDE TO EXCERPTS I've recently answered a few questions regarding excerpts, so I'm going to give a detailed explanation covering as much as I can. PREFACE There seems to be a couple of questions arising from this answer on where the code should go, and the answer is, it is really up to you and how you see fit. There are are a couple of options ...


27

I found this question when looking how to do this without the post object. My additional research turned up this slick technique: $text = apply_filters('the_excerpt', get_post_field('post_excerpt', $post_id));


21

Since WP 3.3.0, wp_trim_words() is helpful if you're able to get the content that you want to generate an excerpt for. Hope that's helpful to someone and it saves creating your own word counting function. http://codex.wordpress.org/Function_Reference/wp_trim_words


21

Since it seems you already have the post object you need the excerpt for, you can just force things to work: setup_postdata( $post ); $excerpt = get_the_excerpt(); The setup_postdata() function will globalize the $post object and make it available for regular old loop function. When you're inside the loop, you call the_post() and it sets things up for you ...


19

In addition to the above filter hook supplied by Deepa's answer here is one additional function that can help you extend the use of the_excerpt in two ways, Allows you to... Limit the excerpt by number of characters but do NOT truncate the last word. This will allow you to return a maximum number of characters but preserve full words, so only the words ...


18

Try this: Create a new function in functions.php and then call it from wherever. function get_excerpt_by_id($post_id){ $the_post = get_post($post_id); //Gets post ID $the_excerpt = $the_post->post_content; //Gets post_content to be used as a basis for the excerpt $excerpt_length = 35; //Sets excerpt length by word count $the_excerpt = ...


18

The codex is your friend and should be your first stop :-) The [...] is added by the_excerpt(). There is a filter supplied called the excerpt_more filter that is specifically included to customize the read more text after the excerpt To remove the [...] after the excerpt text, you can do the following function new_excerpt_more( $more ) { return ''; } ...


15

Turns out the answer was in wp_trim_excerpt(). It's defined in wp-includes/functions.php:1879: /** * Generates an excerpt from the content, if needed. * * The excerpt word amount will be 55 words and if the amount is greater than * that, then the string ' [...]' will be appended to the excerpt. If the string * is less than 55 words, then the content ...


14

Just replace the default output. Make sure you unescape the excerpt before you send it to the editor: add_action( 'add_meta_boxes', array ( 'T5_Richtext_Excerpt', 'switch_boxes' ) ); /** * Replaces the default excerpt editor with TinyMCE. */ class T5_Richtext_Excerpt { /** * Replaces the meta boxes. * * @return void */ public ...


14

add these lines in function.php file function custom_excerpt_length( $length ) { return 20; } add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );


12

Use wp_strip_all_tags() to remove the content of script and style elements too: echo wp_strip_all_tags( get_the_content() );


12

This requires PHP 5.3+ (WP requires PHP 5.2.4+) add_filter('get_the_excerpt', 'end_with_sentence'); function end_with_sentence($excerpt) { $allowed_end = array('.', '!', '?', '...'); $exc = explode( ' ', $excerpt ); $found = false; $last = ''; while ( ! $found && ! empty($exc) ) { $last = array_pop($exc); $end = strrev( $last ); ...


11

What (exactly) happens When calling the_content() inside your template, you are able to call it without any parameters. This means, that the function already has the defaults of null for both arguments: The "more" link text and the boolean switch that allows you to strip teaser content before the "more" link text. The the_content() function calls ...


10

Paste this in your theme's functions.php file add_filter( 'the_excerpt', 'shortcode_unautop'); add_filter( 'the_excerpt', 'do_shortcode');


10

when you fill the Excerpt box with any text on post edit page, the_excerpt() function doesn't add any read more or ... at the end of the short description. Read more is only included if Excerpt is set empty, and this is not a bug, it's a feature. Now the solution is to avoid the excerpt_more filter to return read more, and use the the_excerpt hook. But ...


8

There is a new function since 3.3.0: wp_trim_words I'm using it outside the loop as follows: <?php if ( $post_id ) { $post = get_post( $post_id ); if ( $post ) { ?> <h2><?php echo $post->post_title; ?></h2> <p><em><?php echo wp_trim_words( $post->post_content ); ?></em></p> <p&...


8

Upper right corner in posts editing page > "Screen Options"...there you have to check "Excerpt" ;)


8

Now you can simply use the get_the_excerpt( $postID ) function. Since: WordPress 4.5.0 introduced the $post parameter.


8

You can use the script I found here: http://aaronrussell.co.uk/legacy/improving-wordpress-the_excerpt/ I've modified it to show links in the excerpt, and removed some of the other functions: <?php function keep_my_links($text) { global $post; if ( '' == $text ) { $text = get_the_content(''); $text = apply_filters('the_content', $text); $...


8

You haven't added the second filter, at least not in the code posted. If used, that filter will not print ellipses. // Changing excerpt more function new_excerpt_more($more) { global $post; remove_filter('excerpt_more', 'new_excerpt_more'); return ' <a class="read_more" href="'. get_permalink($post->ID) . '">' . 'read more' . '</a>'; } ...


8

Of course, there's an API function - hidden in the depth of core - to retrieve the parts before and after the <!--more--> tag in the content. Due to exactly zero naming conventions, the handy parts in WP are often overseen: get_extended(). // In the loop, after `the_post()` filled the global `$post`: $info = get_extended( $post ); // The resulting ...


8

I actually never got to add this section to my answer you are referring to. To remove the read more link from the excerpt is quite easy, you just need to compare $count with $excerpt_length. $count will always be between 0 and the value assigned to $excerpt_length. So what we want to do here is to add the read more link in a condition that states that if $...


7

What you're trying to do with the video is exactly what Post Formats were created to handle. Add this to functions: add_theme_support( 'post-formats', array( 'video' ) ); And then this to handle your Read More link: if( !has_post_format( 'video' ) ) { echo '<a href="' . get_permalink() . '">Read More&hellip;</a>'; } else { echo '&...


7

How filters work Filters are simply a point where you can take some value, that is set (for something) in core (for e.g. the the_excerpt() function), in open to modifications. So... $some_var = apply_filters( 'change_this_var', 50 ); ...just says: »Some var« is set to 50, but when you add a filter callback to change_this_var, then you can get, modify ...


7

Another way to get the excerpt, by that I mean the text before the <!--more--> tag, is to use the following function: get_extended() //somewhere in the loop... global $post; $content = get_extended( $post->post_content ); $excerpt = $content['main']; $main_content = apply_filters('the_content', $content['extended']); As it says in the Wordpress ...


7

It's simple, just unregister postexcerpt box first then add another one on the top. Here is my code add_action( 'admin_menu', function () { remove_meta_box('postexcerpt', 'post', 'normal'); }, 999 ); add_action('edit_form_after_title', 'post_excerpt_meta_box');


7

I'm assuming you are using WordPress' Embeds feature. Generally speaking wp_embed_defaults() gives you the information you need. A look at the source discloses the possibilities you have: 2017 /** 2018 * Create default array of embed parameters. 2019 * 2020 * The width defaults to the content width as specified by the theme. If the 2021 *...


6

Just to add to MikeSchinkel's answer, which for some reason wouldn't work for me. I had to add the setup_postdata line to make it work. function get_the_excerpt( $post_id ){ global $post; $save_post = $post; $post = get_post($post_id); setup_postdata( $post ); // hello $output = get_the_excerpt(); $post = $save_post; return $output; } I'...


6

With the recent version of Wordpress (v.3.3.0+), you can actually use wp_trim_words. function excerpt($limit) { return wp_trim_words(get_the_excerpt(), $limit); } See also: https://stackoverflow.com/a/17177847/851045


6

use the filter get_the_excerpt. Look at line no. 250 here, they are using the_excerpt internally on the function get_the_excerpt(), and in this function on line no. 272, they're applying the filter get_the_excerpt on the actual excerpt. Hence, add_filter('get_the_excerpt', 'exc'); function exc($param) { return "Whew !".$param; } is the way to go if ...


Only top voted, non community-wiki answers of a minimum length are eligible