Multiple excerpt lengths in wordpress

2020-01-26 18:51发布

As it says in the title, I'm looking for multiple excerpt lengths in WordPress.

I understand 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 multiple of these each returning different numerical values so I can get short excerpts for sidebar loops, longer excerpts for featured loops, and the longest excerpt for the main article.

Something like using these in the templates:

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

Cheers, Dave

15条回答
姐就是有狂的资本
2楼-- · 2020-01-26 19:30

I think we can now use wp_trim_words see here. Not sure what extra data escaping and sanitization needed to use this function, but it looks interesting.

查看更多
我想做一个坏孩纸
3楼-- · 2020-01-26 19:32

Here an easy way to limit the content or the excerpt

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

change get_the_excerpt() by get_the_content() if you want the contents.

Regards

查看更多
The star\"
4楼-- · 2020-01-26 19:33

This 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 like this.

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

my_excerpt(40); // 40 chars

This is the easiest way that I know of to add filters, that are callable from one function.

查看更多
Bombasti
5楼-- · 2020-01-26 19:33

I thing it is possible to create a short code , i did not try it but i wrote for you the main idea 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 so

[the_excerpt_sc exlength="medium"]
查看更多
神经病院院长
6楼-- · 2020-01-26 19:36

I was looking for this feature as well and most of the functions here are good and flexible. For my own case I was looking for a solution that shows a different excerpt length only on specific pages. I'm using this:

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

Paste this code inside the themes functions.php file.

查看更多
不美不萌又怎样
7楼-- · 2020-01-26 19:36

Here is an article about using custom excerpt length in WordPres. There is a number of Ways To Limit & Control Post Excerpt Length.

  1. Limit post excerpt length or post content length using number of words.
  2. Limiting excerpt length to a number of characters.
  3. Limit post summary by adding ‘read more’ tag.
  4. Enabling custom excerpt to write your own summary for each post.
  5. Control Excerpt Length using Filters

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

查看更多
登录 后发表回答