Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twig+ Wordpress - How to pass array arguments to function?

I'm using a Wordpress theme that was developed using Twig template system. I don't know anything about Twig an don't have the time to learn it.

So my question is, in Wordpress we can use get_terms() to get all the terms from a taxonomy but we can filter the terms we want to receive using an array of arguments that is the second parameter to the function.

That being said, I have a line in a twig file that goes like this:

{% for distrito in wp.get_terms('Distritos') %} 

distrito is my variable and Distritos is my taxonomy name. This works, it calls all of the terms, but I want to use the array arguments so that I can get only the root elements since my taxonomy has hierarchy.

I understand that I must have somewhere the place where wp.get_terms is defined but I can't find it.

like image 247
user2805223 Avatar asked Sep 22 '13 22:09

user2805223


1 Answers

Twig works more or less like plain PHP. For a function call you add parameters like you would in PHP:

{{ method(parameter1, parameter2) }}

Arrays can be defines using [ and ]. Also associative arrays can be defined using { and } like this:

{% set array = [1, 2, 3] %}
{% set assoc = {'key': 'value', 'key2': 2} %}

So your function call should look something like this:

{% for distrito in wp.get_terms('Distritos', ['a', 'b', 'c']) %} 

Checkout the twig docs for further information.

like image 107
ferdynator Avatar answered Nov 19 '22 21:11

ferdynator