Tag Info

Hot answers tagged

15

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


13

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. HTML TAGS/FORMATTING the_excerpt() first of all doesn't accept any parameters, so nothing can be passed to it. It is a fact that the_excerpt() trims the content to 55 words, and all HTML tags are ...


11

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 ); ...


9

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 ...


9

Hi @Robin I. Knight: I view get_the_excerpt() as a function with legacy design. As WordPress usage has grown there are many newer use-cases where it doesn't fit but where the newer functions for getting different data do. One example is the now frequent use of an $args array of function options. But it's easy to fix for your needs. Here's an alternative ...


8

wp_trim_excerpt() has a little curious mechanics - if anything is passed to it then it does nothing. Here is basic logic behind it: get_the_excerpt() checks for manual excerpt; wp_trim_excerpt() chimes in if there is no manual excerpt and makes one from content or teaser. Both are tightly tied to global variables and so Loop. Outside the Loop you are ...


8

Set the filter dynamically based on the page you are loading. If category archive pages have a 100 word excerpt, but posts have a 10 word excerpt, and everything else uses the default: function my_custom_excerpt_length( $orig ) { if( is_category() ) { return 100; } elseif( is_single() ) { return 10; } return $orig; } ...


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 ...


6

@mrwweb is right, post formats are very useful in most cases. As a more generic solution you could combine the_excerpt() and the_content() in one function: function wpse_51699_conditional_excerpt( $more_link_text = null, $stripteaser = false ) { $excerpt = apply_filters( 'the_excerpt', get_the_excerpt() ); $content = get_the_content( ...


6

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 ...


6

there is a field named Excerpt on the post edit page where you can enter your summary and to display make sure you use different template tags. on the front page use <?php the_excerpt(); ?> and on the post page use <?php the_content() ;?> this way you display two different things.


6

The mechanics of excerpt are extremely confusing. It is not precise answer to your question but in general if you need to make template tags, specific to Loop, work with array returned by get_posts() you can emulate Loop like this: $stories = get_posts(); foreach ($stories as $post) { setup_postdata($post); // stuff } wp_reset_postdata();


6

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, ...


5

The template you're actually after is "content.php" You'll want to change this line: <?php if ( is_search() ) : // Only display Excerpts for Search ?> <div class="entry-summary"> <?php the_excerpt(); ?> </div><!-- .entry-summary --> <?php else : ?> <div class="entry-content"> <?php the_content( __( ...


5

Update: Here is a derivative of wp_trim_excerpt() which I used. Works perfectly. Derived from Wordpress version 3.0.4 function my_excerpt($text, $excerpt) { if ($excerpt) return $excerpt; $text = strip_shortcodes( $text ); $text = apply_filters('the_content', $text); $text = str_replace(']]>', ']]&gt;', $text); $text = ...


5

The excerpt filter by default cuts your post by a word count, which I think is probably preferable to a character-based substr function like you're doing, and it strings out tags and images as well while doing it. You can set the number of words to excerpt with the filter excerpt_length (it defaults to 55 words, this function from the codex shows how to ...


5

Ensure that your Theme supports Post Thumbnails, and that the client sets a "Featured Image" for each post. Then, combine the_excerpt() with the_post_thumbnail(), e.g. like so: <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>> <div ...


5

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 = ...


5

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 ...


5

This is one of the reasons why HTML markup is removed from excerpts in the first place, to prevent such issues like this from occurring however, where there's a will, there's a way... You can, through using regular expression, close the open tags applicable to the excerpt only and you might want to take a look at the following link for some ideas, Close ...


4

general: locate the template for the home page - often index.php - if not see http://codex.wordpress.org/Template_Hierarchy find the_content() (possibly with some parameters in the brackets) and change that to the_excerpt() - http://codex.wordpress.org/Function_Reference/the_excerpt specific for Twenty Eleven: index.php points to the template part ...


4

the_excerpt() won't work on a landing page. It must be used inside the post loop in order to function. Inside the loop, WordPress populates a global $post variable with information about the current post. Then, various template tags reference that global variable and output content. Based on your edits above ... You almost have your loop structured ...


4

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> ...


4

WordPress uses the filter wp_trim_excerpt to strip the tags. You can remove the filter and create your own which will allow the links: <?php function new_wp_trim_excerpt($text) { $raw_excerpt = $text; if ( '' == $text ) { $text = get_the_content(''); $text = strip_shortcodes( $text ); $text = apply_filters('the_content', $text); $text ...


4

You can use the default filter for the length of the_excerpt // Changing excerpt length function new_excerpt_length($length) { return 40; } add_filter('excerpt_length', 'new_excerpt_length');


4

Take a look on my answer here: Best Collection of Code for your functions.php file If I understood your question correctly, it does what you are looking for. Place this in functions.php: function excerpt($num) { $limit = $num+1; $excerpt = explode(' ', get_the_excerpt(), $limit); array_pop($excerpt); $excerpt = implode(" ",$excerpt)."... ...


4

Not entirely sure it is supposed to be used like this, but by analogue with the_content try this: add_filter('the_excerpt', array($wp_embed, 'autoembed'), 9);


4

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); ...


4

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


3

The only thing that makes sense to me is that $text immediately after this line: $text = force_balance_tags($out); Has something that wpautop translates as a paragraph break-- something like a double newline. Untested, but I would think that trim would clear it up. $text = trim($text) . $excerpt_end;



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