首页 > 解决方案 > 上面 {{post.content}} 的木材/TWIG 部分,下面休息

问题描述

我需要在上面输出{{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 中)?

标签: phpwordpresstwigtimber

解决方案


不幸的是,目前 Timber 没有为此内置任何内容。我建议为您的帖子编写一个自定义类并制作这些离散函数:

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

这是创建自定义帖子类的指南


推荐阅读