Frizzant
Frizzant

Reputation: 768

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

I need to output let's say the first 100 letters of {{post.content}} somewhere above, and then the second part of {{post.content}} below.

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

The above does not seem to be working for this. I was wondering if there is a elegant solution for this (maybee built in to Timber like the ".length()" )?

Upvotes: 0

Views: 1388

Answers (2)

Jared
Jared

Reputation: 1734

Unfortunately there's nothing built into Timber right now for this. I'd recommend writing a custom class for your post and making these discrete functions:

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

Here's the guide for creating a custom post class

Upvotes: 1

DarkBee
DarkBee

Reputation: 15634

If the content does not contain HTML you just could go with the slice-filter

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

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

{{ lipsum | slice(100) }}

demo

Upvotes: 1

Related Questions