Twig+ Wordpress - How to pass array arguments to function?
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.
Related videos on Youtube
user2805223
Updated on June 09, 2022Comments
-
user2805223 almost 2 years
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 anarray
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 myvariable
andDistritos
is my taxonomy name. This works, it calls all of the terms, but I want to use thearray
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.
-
user2805223 over 10 yearsone more thing, if i want to take each value from this get_terms and get the next level on the same taxonomy i do that, in wordpress by passing the id of parent term to get child. How can i achieve that with twig?