Timber (Twig) how to call wordpress functions
Asked Answered
M

2

7

In Timber is this function

{{post}}

where you get things like

{{post.post_title}}

but there are other functions in WordPress like

get_the_permalink();

which are not accessible from post.get_the_permalink or post.get_permalink

these are the variables i can access

  • 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
  • pinged
  • post_modified
  • post_modified_gmt
  • post_content_filtered
  • post_parent
  • guid
  • menu_order
  • post_type
  • post_mime_type
  • comment_count
  • filter

As this Timber documentation describes there are actions [Timber Doc] but you would have to define each function in the functions.php file ... which means i would have to create each already existing function from php and "redirect" it to the wordpress function. Then bind it to Twig.

Also there was a way as you can see here which is now deprecated

Marchand answered 5/10, 2017 at 20:8 Comment(0)
A
5

First of all, I believe you should use {{post.link}} to get the Permalink.

Second, if you find you often need to call php functions from your twig templates, you might not separate between code and template enough. However, since they allow you to call Wordpress-Actions from the template, you could always write a single action in your functions.php that will, depending on the parameters, call the php-function you want. That would be very similar to the fn() call that was suggested.

Actino answered 5/10, 2017 at 20:36 Comment(3)
i realy dont like the concept of variable functions but i probably need to use them nowMarchand
Use call_user_func if you want it to feel less dirty ;) OTOH, how much do you really need to call from your template, and is that still a template if it's a lot?Actino
yeah i want a clean template but have to use them to get the data where it belongs to .... but anyway i will do it this way ty :)Marchand
M
25

You can call any function by using for example

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

more infos: https://timber.github.io/docs/guides/functions/

Marchand answered 6/10, 2017 at 18:45 Comment(0)
A
5

First of all, I believe you should use {{post.link}} to get the Permalink.

Second, if you find you often need to call php functions from your twig templates, you might not separate between code and template enough. However, since they allow you to call Wordpress-Actions from the template, you could always write a single action in your functions.php that will, depending on the parameters, call the php-function you want. That would be very similar to the fn() call that was suggested.

Actino answered 5/10, 2017 at 20:36 Comment(3)
i realy dont like the concept of variable functions but i probably need to use them nowMarchand
Use call_user_func if you want it to feel less dirty ;) OTOH, how much do you really need to call from your template, and is that still a template if it's a lot?Actino
yeah i want a clean template but have to use them to get the data where it belongs to .... but anyway i will do it this way ty :)Marchand

© 2022 - 2024 — McMap. All rights reserved.