wordpress中的多个摘录长度

dav*_*ker 63 php wordpress function

正如它在标题中所说,我正在寻找WordPress中的多个摘录长度.

我知道你可以在functions.php中做到这一点:

function twentyten_excerpt_length( $length ) {
    return 15;
}
add_filter( 'excerpt_length', 'twentyten_excerpt_length' );
Run Code Online (Sandbox Code Playgroud)

我想知道的是你如何让这些中的多个返回不同的数值,这样我就可以得到侧边栏循环的简短摘录,特色循环的更长摘录,以及主要文章的最长摘录.

在模板中使用这些东西:

<?php the_excerpt('length-short') ?>
<?php the_excerpt('length-medium') ?>
<?php the_excerpt('length-long') ?>
Run Code Online (Sandbox Code Playgroud)

干杯,戴夫

Mar*_*rty 95

怎么样...

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;
}
Run Code Online (Sandbox Code Playgroud)

然后在您的模板代码中,您只需使用..

<?php echo excerpt(25); ?>
Run Code Online (Sandbox Code Playgroud)

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

  • 请看我的答案,它是更新的,并使用内置的wordpress功能. (7认同)
  • 确保你也检查这个函数wp_trim_words http://codex.wordpress.org/Function_Reference/wp_trim_words (6认同)
  • 尼斯.效果很好!谢谢 (3认同)

Mic*_*bak 71

至于现在,你可以升级Marty的回复:

function excerpt($limit) {
    return wp_trim_words(get_the_excerpt(), $limit);
}
Run Code Online (Sandbox Code Playgroud)

您还可以通过以下方式定义自定义"read more"链接:

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());
}
Run Code Online (Sandbox Code Playgroud)

  • 在这种情况下,我认为Stackoverflow需要一个"归档答案并选择新的"类型功能.事情变了哟. (6认同)
  • 值得注意的是,如果你想要一个'$ limit`超过55个单词,你也必须调整默认的摘录长度.`function custom_excerpt_length($ length){return 135; } add_filter('excerpt_length','custom_excerpt_length',999);` (4认同)
  • 到目前为止最好和最简单的回应.优秀使用内置WP功能.感谢您不要过度设计解决方案. (3认同)
  • 甜蜜 - 这是要走的路.并且响应Tri Nguyen对数据清理的担忧:`wp_trim_words()`在文本上调用`wp_strip_all_tags()`,所以不用担心在帖子内容中破坏HTML. (2认同)

Bay*_*ae' 26

这就是我提出的.

将此添加到您的 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);
}
Run Code Online (Sandbox Code Playgroud)

它可以像这样使用.

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

my_excerpt(40); // 40 chars
Run Code Online (Sandbox Code Playgroud)

这是我所知道的添加过滤器的最简单方法,可以从一个函数调用.

  • 一个不错的解决方案,但是花了我一段时间才能使它工作,因为它缺乏在当前(2013)版本的Wordpress中有效的优先权:使用`add_filter('excerpt_length','Excerpt :: new_length',999);`代替(注意最后一个参数) (2认同)

小智 10

我也在寻找这个功能,这里的大多数功能都很好而且灵活.对于我自己的情况,我一直在寻找一种解决方案,只在特定页面上显示不同的摘录长度.我正在使用这个:

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

将此代码粘贴到主题functions.php文件中.


小智 5

回到Marty的回复:

我知道自从这个回复发表以来已经有一年多了,但是迟到总比没有好.为了使用超过WordPress默认值55的限制,您需要替换此行:

     $excerpt = explode(' ', get_the_excerpt(), $limit);
Run Code Online (Sandbox Code Playgroud)

用这一行:

     $excerpt = explode(' ', get_the_content(), $limit);
Run Code Online (Sandbox Code Playgroud)

否则,该功能仅适用于已修剪过的文本.


Mik*_*ace 5

您可以在您的functions.php文件中添加此功能

function custom_length_excerpt($word_count_limit) {
    $content = wp_strip_all_tags(get_the_content() , true );
    echo wp_trim_words($content, $word_count_limit);
}
Run Code Online (Sandbox Code Playgroud)

然后在你的模板中调用它

<p><?php custom_length_excerpt(50); ?>
Run Code Online (Sandbox Code Playgroud)

wp_strip_all_tags应防止杂散html标签断裂的页面.


有关功能的文档

  • 有没有办法可以使用上面的例子跳过html代码?在我的一些页面中可以看到短代码 (3认同)