Tag Info

Hot answers tagged

35

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


18

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


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


10

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

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

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


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


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

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


8

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


7

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


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

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


6

Try this: if ( has_excerpt( $some_post_id ) ) { // Do something here? } else { // Or here? } References: has_excerpt


6

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


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

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


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


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

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


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

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


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

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


5

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


5

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


4

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


4

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


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



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