A few excerpts in wordpress

As the title says, I'm looking for a few excerpts in WordPress.

I understand that you can do this in functions.php:

function twentyten_excerpt_length( $length ) { return 15; } add_filter( 'excerpt_length', 'twentyten_excerpt_length' ); 

What I want to know is how you can have several of them, each of which returns different numerical values, so I can get short excerpts for sidebar loops, longer excerpts for selected loops and the longest excerpt for the main article.

Something like using these patterns:

 <?php the_excerpt('length-short') ?> <?php the_excerpt('length-medium') ?> <?php the_excerpt('length-long') ?> 

Cheers, dave

+57
function php wordpress
Nov 02 2018-10-11T00:
source share
15 answers

What about...

 function excerpt($limit) { $excerpt = explode(' ', get_the_excerpt(), $limit); if (count($excerpt) >= $limit) { array_pop($excerpt); $excerpt = implode(" ", $excerpt) . '...'; } else { $excerpt = implode(" ", $excerpt); } $excerpt = preg_replace('`\[[^\]]*\]`', '', $excerpt); return $excerpt; } function content($limit) { $content = explode(' ', get_the_content(), $limit); if (count($content) >= $limit) { array_pop($content); $content = implode(" ", $content) . '...'; } else { $content = implode(" ", $content); } $content = preg_replace('/\[.+\]/','', $content); $content = apply_filters('the_content', $content); $content = str_replace(']]>', ']]&gt;', $content); return $content; } 

then in the code of your template you just use.

 <?php echo excerpt(25); ?> 

from: http://bavotasan.com/tutorials/limiting-the-number-of-words-in-your-excerpt-or-content-in-wordpress/

+84
Nov 03 '10 at 11:24
source share

Now you can update Marty's answer:

 function excerpt($limit) { return wp_trim_words(get_the_excerpt(), $limit); } 

You can also set up a custom read more link:

 function custom_read_more() { return '... <a class="read-more" href="'.get_permalink(get_the_ID()).'">more&nbsp;&raquo;</a>'; } function excerpt($limit) { return wp_trim_words(get_the_excerpt(), $limit, custom_read_more()); } 
+54
Jun 18 '13 at 20:02
source share

Here is what I came up with.

Add this to your functions.php

 class Excerpt { // Default length (by WordPress) public static $length = 55; // So you can call: my_excerpt('short'); public static $types = array( 'short' => 25, 'regular' => 55, 'long' => 100 ); /** * Sets the length for the excerpt, * then it adds the WP filter * And automatically calls the_excerpt(); * * @param string $new_length * @return void * @author Baylor Rae' */ public static function length($new_length = 55) { Excerpt::$length = $new_length; add_filter('excerpt_length', 'Excerpt::new_length'); Excerpt::output(); } // Tells WP the new length public static function new_length() { if( isset(Excerpt::$types[Excerpt::$length]) ) return Excerpt::$types[Excerpt::$length]; else return Excerpt::$length; } // Echoes out the excerpt public static function output() { the_excerpt(); } } // An alias to the class function my_excerpt($length = 55) { Excerpt::length($length); } 

It can be used as follows.

 my_excerpt('short'); // calls the defined short excerpt length my_excerpt(40); // 40 chars 

This is the easiest way to add filters that can be called from a single function.

+24
Nov 02 '10 at
source share

I also searched for this feature, and most of the features here are nice and flexible. For my case, I was looking for a solution that shows a different fragment length only on certain pages. I use this:

 function custom_excerpt_length( $length ) { return (is_front_page()) ? 15 : 25; } add_filter( 'excerpt_length', 'custom_excerpt_length', 999 ); 

Paste this code into the functions.php theme file.

+10
Aug 30 '13 at 13:57 on
source share

Returning to Marty's answer:

I know that more than a year has passed since this answer was published, but better late than never. To work with the default limitations of WordPress 55, you need to replace this line:

  $excerpt = explode(' ', get_the_excerpt(), $limit); 

with this line:

  $excerpt = explode(' ', get_the_content(), $limit); 

Otherwise, the function only works with the already trimmed piece of text.

+5
Dec 24 2018-11-11T00:
source share

I think now we can use wp_trim_words here . I'm not sure what additional screening and disinfection features are needed to use this feature, but it looks interesting.

+4
Aug 07
source share

You can add this function to your functions.php file.

 function custom_length_excerpt($word_count_limit) { $content = wp_strip_all_tags(get_the_content() , true ); echo wp_trim_words($content, $word_count_limit); } 

Then name it in your template as follows

 <p><?php custom_length_excerpt(50); ?> 

wp_strip_all_tags should prevent html shadow tags from breaking.




Feature Documentation

+4
Apr 25 '14 at 23:22
source share

Here's an easy way to limit content or exposure.

 $content = get_the_excerpt(); $content = strip_tags($content); echo substr($content, 0, 255); 

change get_the_excerpt () to get_the_content () if you want the content.

Hello

+2
Mar 11 '15 at 9:04
source share

I found a great plugin that can do this - Content and Excerpt Word Limit

0
Nov 04 '10 at 16:27
source share

Use extended snippet
http://wordpress.org/extend/plugins/advanced-excerpt/ plugin. I also found the answer from this page.

0
Apr 23 '13 at 15:54
source share

I can create a short code, I have not tried it, but I wrote the basic idea for you about its structure

 function twentyten_excerpt_length($atts,$length=null){ shortcode_atts(array('exlength'=>'short'),$atts); if(!isset($atts['exlength']) || $atts['exlength'] == 'short') { return 15; }elseif( $atts['exlength'] == 'medium' ){ return 30; // or any value you like }elseif( $atts['exlength'] == 'long' ){ return 45; // or any value you like }else{ // return nothing } } add_shortcode('the_excerpt_sc','twentyten_excerpt_length'); 

so you can use it like that

 [the_excerpt_sc exlength="medium"] 
0
Jun 17 '13 at 11:29
source share

I know this is a really old thread, but I just struggled with this problem and none of the solutions I found on the Internet worked correctly for me. First, my own excerpt_more filter has always been cut off.

The way I decided is ugly, but this is the only working solution I could find. The ugliness involves modifying the 4 lines of the WP kernel (!!) + using another global variable (although WP already does this so that I don't feel too bad).

I changed wp_trim_excerpt to wp-includes / formatting.php:

 <?php function wp_trim_excerpt($text = '') { global $excerpt_length; $len = $excerpt_length > 0 ? $excerpt_length : 55; $raw_excerpt = $text; if ( '' == $text ) { $text = get_the_content(''); $text = strip_shortcodes( $text ); $text = apply_filters('the_content', $text); $text = str_replace(']]>', ']]&gt;', $text); $excerpt_length = apply_filters('excerpt_length', $len); $excerpt_more = apply_filters('excerpt_more', ' ' . '[&hellip;]'); $text = wp_trim_words( $text, $excerpt_length, $excerpt_more ); } $excerpt_length = null; return apply_filters('wp_trim_excerpt', $text, $raw_excerpt); } 

The only new material is the $excerpt_length and $len bits.

Now, if I want to change the default length, I do this in my template:

 <?php $excerpt_length = 10; the_excerpt() ?> 

Changing the kernel is a terrible decision, so I would like to know if someone comes up with something better.

0
Aug 30 '13 at 0:19
source share

Be careful using some of these methods. Not all of them supplant html tags, that is, if someone inserts a link to a video (or url) in the first sentence of his message, the video (or link) will be displayed in an excerpt, possibly inflating your page.

0
Oct. 15 '13 at 14:04 on
source share

I would do the following:

 function _get_excerpt($limit = 100) { return has_excerpt() ? get_the_excerpt() : wp_trim_words(strip_shortcodes(get_the_content()),$limit); } 

Using:

 echo _get_excerpt(30); // Inside the loop / query 

Why?

  • If has_excerpt should return a given passage
  • It is not, therefore, truncate words / shorten the_content from the_content
0
May 30 '17 at 9:08
source share

Here 's an article about using custom shutter speeds in WordPres. There are several ways to limit and shutter speed for control messages.

  • Limit tear-off length or message length using word count.
  • Limit shutter speed to a few characters.
  • Limit post message by adding 'read more tag.
  • Include custom excerpts for writing your own resume for each post.
  • Filter expression control length

http://smallenvelop.com/limit-post-excerpt-length-in-wordpress/ I hope this helps you a lot.

-one
Sep 13 '16 at 6:38
source share



All Articles