php - 上面 {{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 中)?
解决方案
不幸的是,目前 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;
}
推荐阅读
- javascript - 访问 dataLayer 中的数组并发送到自定义维度
- vb.net - Visual Studio 自动生成的代码删除
- flutter - Riverpod:重新计算提供者在接收更新时提供项目列表
- sql - 计算插入值的函数
- bash - Azure CLI 停止/启动所有连续的 webjobs 脚本
- c++ - 单链表中的堆与堆栈分配的节点对象
- javascript - 错误:“已为此容器注册了另一个导航器。您可能在单个导航容器下有多个导航器”
- c++ - 如果 constexpr not-taken 分支不需要有效,确定吗?
- docker - 移动正在运行的容器以及容器产生的数据
- c++ - 以 root 权限在 Netbeans 上运行远程(Pi3b+)项目