Timber/TWIG 部分 {{post.content}} 以上,其余部分在下方

Timber/TWIG part of {{post.content}} above, rest below

我需要输出比方说 {{post.content}} 上面某处的前 100 个字母,然后是 { {post.content}} 下面。

{{post.content.length(100)}}  //to display the first 100 characters
{{post.content.length(-100)}} //to remove the first 100 characters

以上似乎不适用于此。我想知道是否有一个优雅的解决方案(也许像 ".length()" 一样内置到 Timber 中的蜜蜂)?

如果内容不包含 HTML 你可以使用 slice-filter

{{ lipsum | slice(0, 100) }}

-----------------------------------------

{{ lipsum | slice(100) }}

demo

遗憾的是,目前 Timber 中没有为此内置任何内容。我建议为您的 post 编写自定义 class 并制作这些离散函数:

<?php

  class MyPost extends Timber\Post {

    function content_top() {
        //first grab what WP has in the database
        $content = $this->post_content;     

        //do stuff here to get first 100 chars

        //apply WP's filters
        $content = apply_filters('the_content', ($content));
        return $content;
    }

   function content_bottom() {
        //first grab what WP has in the database
        $content = $this->post_content;     

        //do stuff here to get last 100 chars

        //apply WP's filters
        $content = apply_filters('the_content', ($content));
        return $content;
    }

这里是the guide for creating a custom post class