【问题标题】:Wordpress: custom excerpt lengthWordpress:自定义摘录长度
【发布时间】:2013-02-26 21:18:29
【问题描述】:

我试图弄清楚是否有可能从每个帖子中获取摘录,从每个帖子中获取第一段。我目前正在使用 ACF 插件并具有自定义帖子类型和自定义字段。 这是我的代码:

function custom_field_excerpt() {
    global $post;
    $text = get_field('news');
    if ( '' != $text ) {
        $text = strip_shortcodes( $text );
        $text = apply_filters('the_content', $text);
        $text = str_replace(']]>', ']]>', $text);
        $excerpt_length = 20; // 20 words
        $text = wp_trim_words( $text, $excerpt_length, $excerpt_more );
    }
    return apply_filters('the_excerpt', $text);
}

这很好用,但它只修剪前 20 个单词(或您指定的任何单词),我正在尝试调整它以拉入每个帖子的第一段而不是前 20 个单词。这有可能吗?

【问题讨论】:

    标签: php wordpress function


    【解决方案1】:

    因为我们知道内容是裸露的(只是文本),您可以简单地通过 \n 字符展开内容,然后假设新数组中的第一个元素是第一段。您也许可以以更有效的方式执行此操作,但功能如下:

    你的新功能

    function custom_field_excerpt() {
      global $post;
      $text = get_field('news');
      if ( '' != $text ) {
        $text = strip_shortcodes( $text );
        $text = apply_filters('the_content', $text);
        $text = str_replace(']]>', ']]>', $text);
        $text_paragraphs = explode("\n",$text);
        $text = $text_paragraphs[0];
      }
      return $text;
    }
    

    【讨论】:

    • 为答案喝彩,但不完全是。内容正在像This is an excerpt and it is twenty words long this is an excerpt and it is twenty words long... 这样返回。无论段落如何,它只是从每个帖子中提取前 20 个单词。我正在尝试从每个帖子中提取第一段,一个可能是 50 个字长,一个可能是 30 个等等。我尝试了上面的函数,但它返回了帖子中的所有内容而不是第一段。
    • 是在

      标签中返回的内容,还是您在模板中手动添加这些内容?我在想最后可能只是return $text;,因为我不确定apply_filters('the_excerpt',$text); 会对我的编辑做什么。

    • 不是摘录,没有。基本上每个帖子都有自己的页面,这些都包含在 p 标签中,但是存档页面,我想显示每个帖子的标题和一小段摘录(第一段),但摘录去掉了任何文本格式,包括 p标签,所以我不确定是否可以抓住第一段。
    • 那么您可以简单地在您的文本中找到\n,这可能有效吗?尝试将我的explode 语句切换为:explode("\n",$text); 注意:这些是\n 周围的双引号,并去掉下一行的.'</p>'。让我知道情况如何。
    【解决方案2】:

    这是正确的代码,将其添加到您的functions.php文件中

    // Add custom excerpt length
    function custom_excerpt($excerpt_length) {
        $content = get_field('custom_field_name');
            $text = strip_shortcodes( $content );
            //$text = apply_filters('the_content', $text);
            $text = str_replace(']]>', ']]>', $text);
            $text = strip_tags($text);
            $words = preg_split("/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY);
            if ( count($words) > $excerpt_length ) {
                array_pop($words);
                $text = implode(' ', $words);
                $text = $text . $excerpt_more;
            } else {
                $text = implode(' ', $words);
            }
    
        echo $text;
    }
    

    然后使用 指定模板中的长度 (去掉上面php标签中的空格)

    对于其他可能发现这一点的人 - 如果您使用的是 the_content 而不是 ACF,那么只需更改 $content = get_field('blog_article_text'); 至 $content = get_the_content();

    【讨论】:

      【解决方案3】:

      尝试将 $excerpt_length 替换为 strlen($text)

              $text = wp_trim_words( $text, strlen($text), $excerpt_more );
      

      【讨论】:

      • 不,这只是从帖子中提取所有文本而不是第一段?
      • 如果第一段是 $post 你可以在 strlen() 中替换它
      猜你喜欢
      • 2018-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-18
      • 2011-05-04
      • 1970-01-01
      • 2017-02-26
      相关资源
      最近更新 更多