13

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.

3
  • I think you have to find out how it is implemented... Maybe using a plugin? Or by hand? How are templates being rendered? Seems like you're going to have to learn Twig after all... Commented Sep 22, 2013 at 22:17
  • hi, thanks for the sugestion but the thing is,even twig itself doesn't have specific tutorials on getting it together with wordpress. I'm hopping someone can show me the direction, only that. Commented Sep 22, 2013 at 22:22
  • Well, Twig is just a templating engine. It does need some glue to make it work with WordPress seamlessly but it doesn't have any dependencies. So you'd have to find the glue to understand how it's working on that particular theme... Maybe it's using this plugin github.com/jarednova/timber? Commented Sep 22, 2013 at 22:24

1 Answer 1

25

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.

Sign up to request clarification or add additional context in comments.

1 Comment

one 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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.