【问题标题】:Multiple excerpt lengths in wordpresswordpress中的多个摘录长度
【发布时间】:2011-05-04 05:13:04
【问题描述】:

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

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

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

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

类似于在模板中使用这些:

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

干杯, 戴夫

【问题讨论】:

    标签: php wordpress function


    【解决方案1】:

    怎么样...

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

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

    <?php echo excerpt(25); ?>
    

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

    【讨论】:

    • 确保你也检查了这个功能 wp_trim_words codex.wordpress.org/Function_Reference/wp_trim_words
    • 请看我的答案,它是最新的并且使用了内置的 wordpress 功能。
    • 现在有更简单的方法可以使用更新版本的 WordPress。
    • 为什么不添加一个额外的输入,这样你也可以改变“阅读更多”文本?
    【解决方案2】:

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

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

    您还可以通过这种方式定义自定义“阅读更多”链接:

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

    【讨论】:

    • 迄今为止最好和最简单的回应。出色地使用了内置的 WP 功能。感谢您没有过度设计解决方案。
    • @Michal,一段很棒的代码,没想到居然这么简单
    • 甜蜜 - 这是要走的路。为了回应 Tri Nguyen 对数据清理的担忧:wp_trim_words() 在文本上调用 wp_strip_all_tags(),因此不必担心在帖子内容中破坏 HTML。
    • 值得注意的是,如果您希望$limit 超过 55 个字,您还必须调整默认摘录长度。 function custom_excerpt_length( $length ) { return 135; } add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
    • 在这种时候,我认为 Stackoverflow 需要一个“存档答案并选择新的”类型的功能。事情改变了哟。
    【解决方案3】:

    这是我想出来的。

    将此添加到您的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);
    }
    

    可以这样使用。

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

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

    【讨论】:

    • 我更喜欢这个答案,因为它是编写代码的好方法。谢谢贝勒。
    • 不错的解决方案,但我花了一段时间才让它工作,因为它缺乏在当前(2013)版本的 Wordpress 中有效的优先级:改用add_filter('excerpt_length', 'Excerpt::new_length', 999);(注意最后一个参数)跨度>
    【解决方案4】:

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

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

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

    【讨论】:

      【解决方案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);
      }
      

      然后像这样在你的模板中调用它

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

      wp_strip_all_tags 应该防止杂散的 html 标签破坏页面。


      函数文档

      【讨论】:

      • 有没有办法使用上面的示例跳过 html 代码?短代码在我的一些页面中可见
      【解决方案6】:

      回到Marty的回复:

      我知道这篇回复发表已经一年多了,但迟到总比没有好。要使其在超过 WordPress 默认值 55 的限制下工作,您需要替换此行:

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

      用这一行:

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

      否则,该函数仅适用于已经修剪过的文本。

      【讨论】:

        【解决方案7】:

        我想我们现在可以使用wp_trim_words see here。 不确定使用此功能需要哪些额外的数据转义和清理,但它看起来很有趣。

        【讨论】:

          【解决方案8】:

          这是一种限制内容或摘录的简单方法

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

          如果您想要内容,请通过 get_the_content() 更改 get_the_excerpt()。

          问候

          【讨论】:

            【解决方案9】:

            小心使用其中一些方法。并非所有人都去掉了 html 标签,这意味着如果有人在他们帖子的第一句话中插入指向视频(或 url)的链接,视频(或链接)将出现在摘录中,可能会炸毁你的页面。

            【讨论】:

            • 欢迎来到 SO。这需要评论,而不是回答。耐心等待,您将获得评论权。
            • 谢谢马克西姆斯,我很抱歉。
            【解决方案10】:

            我会这样做:

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

            用法:

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

            为什么?

            • 如果has_excerpt 应该返回给定摘录
            • 不是,所以 修剪单词 / 去除短代码来自the_content

            【讨论】:

            • 这不是 get_the_excerpt 函数自动执行的操作吗?你的函数并没有真正修剪现有的摘录......
            【解决方案11】:

            我认为可以创建一个短代码,我没有尝试过,但我为你写了关于它的结构的主要思想

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

            所以你可以像这样使用它

            [the_excerpt_sc exlength="medium"]
            

            【讨论】:

              【解决方案12】:

              我知道这是一个非常古老的线程,但我只是在这个问题上苦苦挣扎,我在网上找到的解决方案都没有适合我。一方面,我自己的“excerpt_more”过滤器总是被切断。

              我解决它的方法很丑陋,但这是我能找到的唯一可行的解​​决方案。丑陋之处在于修改 4 行 WP core(!!) + 使用另一个全局变量(虽然 WP 已经做了很多我不觉得太糟糕)。

              我将 wp-includes/formatting.php 中的 wp_trim_excerpt 更改为:

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

              唯一的新东西是$excerpt_length$len 位。

              现在,如果我想更改默认长度,请在我的模板中执行此操作:

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

              更改核心是一个糟糕的解决方案,所以我很想知道是否有人提出了更好的解决方案。

              【讨论】:

              • 嗯...为什么Michal's solution(使用wp_trim_words())对你不起作用?比破解核心要好得多...
              • 嗯..我写这篇文章的时候一定错过了。肯定会在我目前的 WP 项目中尝试一下。我之前在使用 WP 时遇到过问题,尽管在使用 get_the_* 函数而不是 the_* 函数时它们不会返回完全相同的东西(根据我的经验,the_content()get_the_content()
              【解决方案13】:

              我写了an article 关于在 WordPress 中使用自定义摘录长度。 有多种方法可以限制和控制文章摘录的长度。

              1. 使用字数限制帖子摘录长度或帖子内容长度。
              2. 将摘录长度限制为多个字符。
              3. 通过添加“阅读更多”标签来限制帖子摘要。
              4. 启用自定义摘录为每篇文章编写自己的摘要。
              5. 使用过滤器控制摘录长度

              希望对你有很大帮助。

              【讨论】:

                猜你喜欢
                • 2017-02-26
                • 1970-01-01
                • 1970-01-01
                • 2018-04-19
                • 2018-01-23
                • 2014-07-31
                • 2014-04-12
                • 2018-12-30
                相关资源
                最近更新 更多