查看Wordpress源码可以发现  excerpt_length 默认长度为 55 ,

对应的代码在文件:wp-includes/formatting.php

function wp_trim_excerpt( $text = '' ) {
   $raw_excerpt = $text;
   if ( '' == $text ) {
      $text = get_the_content('');

      $text = strip_shortcodes( $text );

      /** This filter is documented in wp-includes/post-template.php */
      $text = apply_filters( 'the_content', $text );
      $text = str_replace(']]>', ']]>', $text);

      /**
       * Filters the number of words in an excerpt.
       *
       * @since 2.7.0
       *
       * @param int $number The number of words. Default 55.
       */
      $excerpt_length = apply_filters( 'excerpt_length', 55 );
      /**
       * Filters the string in the "more" link displayed after a trimmed excerpt.
       *
       * @since 2.9.0
       *
       * @param string $more_string The string shown within the more link.
       */
      $excerpt_more = apply_filters( 'excerpt_more', ' ' . '[…]' );
      $text = wp_trim_words( $text, $excerpt_length, $excerpt_more );
   }

从代码中同样可以看出,可以通过add_filters( ‘excerpt_length’,function(){}); 来重置摘要长度这个值;

具体操作是在当前主题的functions.php 中添加 excerpt_length 的  filter;

function my_excerpt_length( $length ) {
   return 500;//指定的摘要长度
}
add_filter( 'excerpt_length', 'my_excerpt_length' );

 

打赏
excerpt_length 调整 WordPress 文章摘要长度
Tagged on:

发表评论