php wordpress 中的多个摘录长度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4082662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:54:01  来源:igfitidea点击:

Multiple excerpt lengths in wordpress

phpwordpressfunction

提问by davebowker

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

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

I understand you can do this in functions.php:

我知道你可以在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

干杯,戴夫

回答by Marty

How 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 your template code you just use..

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

<?php echo excerpt(25); ?>

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

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

回答by Micha? Rybak

As for now, you can upgrade Marty's reply:

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

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

You can also define custom 'read more' link this way:

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

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

回答by Baylor Rae'

This is what I came up with.

这就是我想出的。

Add this to your functions.php

将此添加到您的 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.

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

回答by Olaf

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.

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

回答by Mike Grace

You can add to your functions.php file this function

你可以在你的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);
}

Then call it in your template like this

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

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

The wp_strip_all_tagsshould prevent stray html tags from breaking the page.

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



Documentation on functions

函数文档

回答by 0x61696f

Going back to Marty's reply:

回到马蒂的回复:

I know it's been well over a year since this reply got published, but it's better late than never. For this to work with limits of over the WordPress default of 55, you need to replace this line:

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

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

with this line:

用这一行:

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

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

否则,该功能仅适用于已经修剪过的一段文本。

回答by Tri Nguyen

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

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

回答by Mohammed

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.

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

Regards

问候

回答by Doug Hucker

Be careful using some of these methods. Not all of them strip the html tags out, meaning if someone inserts a link to a video (or url) in the first sentence of their post, the video (or link) will show up in the excerpt, possibly blowing up your page.

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

回答by powerbuoy

I know this is a really old thread, but I just struggled with this problem and none of the solutions I found online worked properly for me. For one thing my own "excerpt_more"-filter was always cut off.

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

The way I solved it is ugly as hell, but it's the only working solution I could find. The ugliness involves modifying 4 lines of WP core(!!) + the use of yet another global variable (although WP already does this so much I don't feel too bad).

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

I changed wp_trim_excerptin wp-includes/formatting.php to this:

wp_trim_excerpt在 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 stuff is the $excerpt_lengthand $lenbits.

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

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

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

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

Changing core is a horrible solution so I'd love to know if someone comes up with something better.

改变核心是一个可怕的解决方案,所以我很想知道是否有人想出了更好的办法。