wordpress中的多个摘录长度

时间:2010-11-02 22:27:47

标签: php wordpress function

正如标题中所说,我正在寻找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') ?>

干杯, 戴夫

15 个答案:

答案 0 :(得分:94)

怎么样......

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/

答案 1 :(得分:67)

至于现在,你可以升级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());
}

答案 2 :(得分: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);
}

可以像这样使用。

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

my_excerpt(40); // 40 chars

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

答案 3 :(得分:10)

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

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

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

答案 4 :(得分:5)

回到Marty的回复:

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

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

这一行:

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

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

答案 5 :(得分: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标记破坏页面。


有关职能的文件

答案 6 :(得分:4)

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

答案 7 :(得分:3)

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

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

如果你想要内容,可以通过get_the_content()更改get_the_excerpt()。

此致

答案 8 :(得分:1)

小心使用其中一些方法。并非所有这些都删除了html标签,这意味着如果有人在帖子的第一句话中插入了视频(或网址)的链接,则视频(或链接)将显示在摘录中,可能会炸毁您的页面。

答案 9 :(得分:0)

我找到了一个很棒的插件,可以做到这一点 - Content and Excerpt Word Limit

答案 10 :(得分:0)

使用高级摘录
http://wordpress.org/extend/plugins/advanced-excerpt/ 插入。我也从这个页面找到了答案。

答案 11 :(得分:0)

我觉得有可能创建一个简短的代码,我没试过,但我为你写了关于它的结构的主要想法

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 :(得分:0)

我知道这是一个非常古老的主题,但我只是在努力解决这个问题,我在网上找到的解决方案都没有为我正常工作。一方面,我自己的“excerpt_more”-filter总是被切断。

我解决它的方式很难看,但它是我能找到的唯一可行的解​​决方案。丑陋涉及修改4行WP核心(!!)+使用另一个全局变量(尽管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() ?>

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

答案 13 :(得分:0)

我会这样做:

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

答案 14 :(得分:-1)

Here是一篇关于在WordPres中使用自定义摘录长度的文章。 有许多方法限制&amp;控制后摘录长度。

  1. 使用字数限制帖子摘录长度或发布内容长度。
  2. 将摘录长度限制为多个字符。
  3. 通过添加“了解更多”标记来限制帖子摘要。
  4. 启用自定义摘录以为每个帖子编写自己的摘要。
  5. 使用过滤器控制摘录长度
  6. http://smallenvelop.com/limit-post-excerpt-length-in-wordpress/ 我希望这会对你有所帮助。

相关问题