Timber(Twig)如何调用wordpress函数

时间:2017-10-05 20:08:08

标签: php wordpress twig timber twig-extension

Timber就是这个功能

{{post}}

你得到像

这样的东西
{{post.post_title}}

但WordPress中还有其他功能,如

get_the_permalink();

无法从post.get_the_permalink或post.get_permalink

访问

这些是我可以访问的变量

  • ID
  • post_author
  • POST_DATE
  • post_date_gmt
  • POST_CONTENT
  • POST_TITLE
  • post_excerpt
  • post_status
  • comment_status
  • ping_status
  • post_password
  • POST_NAME
  • to_ping
  • 执行ping操作
  • post_modified
  • post_modified_gmt
  • post_content_filtered
  • post_parent
  • GUID
  • menu_order
  • post_type
  • post_mime_type
  • COMMENT_COUNT
  • 过滤

正如这篇Timber文档描述的那样,有动作[Timber Doc],但你必须在functions.php文件中定义每个函数...这意味着我必须从php创建每个已经存在的函数并“重定向”它到wordpress功能。然后将它绑定到Twig。

还有一种方法可以看到here现在是deprecated

2 个答案:

答案 0 :(得分:9)

您可以使用例如

来调用任何函数
{{ function('get_permalink', post.ID) }}

更多信息: https://timber.github.io/docs/guides/functions/

答案 1 :(得分:3)

首先,我相信您应该使用{{post.link}}获取永久链接。

其次,如果您发现经常需要从twig模板中调用php函数,则可能无法在代码和模板之间进行足够的分离。 但是,因为它们允许您从模板中调用Wordpress-Actions,所以您总是可以在functions.php中编写一个动作,根据参数调用您想要的php函数。这与建议的fn()电话非常相似。