【问题标题】:Timber (Twig) how to call wordpress functionsTimber(Twig)如何调用wordpress函数
【发布时间】:2018-03-17 13:33:54
【问题描述】:

Timber 中有这个功能

{{post}}

你从哪里得到类似的东西

{{post.post_title}}

但WordPress中还有其他功能,例如

get_the_permalink();

不能从 post.get_the_permalink 或 post.get_permalink 访问

这些是我可以访问的变量

  • 身份证
  • 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
  • 指导
  • menu_order
  • post_type
  • post_mime_type
  • comment_count
  • 过滤器

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

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

【问题讨论】:

    标签: php wordpress twig timber twig-extension


    【解决方案1】:

    你可以使用例子来调用任何函数

    {{ function('get_permalink', post.ID) }}
    

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

    【讨论】:

      【解决方案2】:

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

      其次,如果你发现你经常需要从你的 twig 模板中调用 php 函数,那么你可能没有将代码和模板充分分开。 但是,由于它们允许您从模板调用 Wordpress-Actions,因此您始终可以在 functions.php 中编写单个操作,该操作将根据参数调用您想要的 php-function。这与建议的fn() 调用非常相似。

      【讨论】:

      • 我真的不喜欢variable functions 的概念,但我现在可能需要使用它们
      • 如果您想让它感觉不那么脏,请使用 call_user_func ;) OTOH,您真的需要从您的模板中调用多少,如果它很多,那仍然是一个模板吗?
      • 是的,我想要一个干净的模板,但必须使用它们来获取它所属的数据....但无论如何我都会这样做:)
      猜你喜欢
      • 2015-10-24
      • 1970-01-01
      • 2020-08-31
      • 1970-01-01
      • 1970-01-01
      • 2019-01-09
      • 2016-12-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多