> For the complete documentation index, see [llms.txt](https://partners.tray.com.br/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://partners.tray.com.br/themes/construindo-seu-template/referencias/funcoes/categories.md).

# Categories()

A função `Categories()` retorna as categorias cadastradas no Painel Administrativo da loja.

{% hint style="info" %}
**Disponível em todas as páginas.**\
**Para saber mais sobre como cadastrar categorias acesse:** [Cadastrar Categorias](https://basedeconhecimento.tray.com.br/hc/pt-br/articles/6734145561243-Cadastrar-Categorias)
{% endhint %}

## Como Usar

As requisições de categorias sempre retornarão um array de dados, veja o padrão de retorno em: [categories](/themes/construindo-seu-template/referencias/objetos/categories.md).

### Para Exibir Todas as Categorias

No código abaixo a função Categories() é atribuída a uma variável e a partir dela é executado um loop para exibir o nome de cada categoria existente.

{% code title="Input" %}

```html
{# todas as categorias #}
<ul>
  {% set categories = Categories() %}
  {% for categorie in categories %}
    <li>{{ categorie.name }}</li>
  {% endfor %}
</ul>
```

{% endcode %}

{% code title="Output" %}

```html
<ul>
    <li>Acessórios</li>
    <li>Roupas</li>
    <li>Calçados</li>
    <li>Outlet</li>
</ul>
```

{% endcode %}

{% hint style="info" %}
Ao atribuir a função **`Categories()`** a variável categories ela retorna todas as categorias independente de possuírem produtos cadastrados nelas ou não.
{% endhint %}

### Para Buscar determinada Categoria por ID

No código abaixo é atribuída a uma variável a função Categories() passando como parâmetro o id desejado de valor 2, e então é exibido o nome dessa categoria de id 2.

{% code title="Input" %}

```html
{# busca o id 2: #}
{% set categoryTray = Categories( { "id": 2 } ) %}
<span>{{ categoryTray.name }}</span>    
```

{% endcode %}

{% code title="Output" %}

```html
<span>Roupas</span> 
```

{% endcode %}

### Para Exibir todas categorias em ordem ascendente

No código abaixo é atribuída a uma variável a função `Categories()` passando como parâmetro um objeto que define a ordem de exibição das categorias através do nome como ascendente, e então com o loop percorrendo todas categorias é exibido o nome de todas categorias ordenadas.

{% code title="Input" %}

```html
{# todas as categorias ordenadas por nome ascendente #}
{% set myCategories = Categories({ "order": { "name": "asc" }}) %}
<ul>
  {% for category in myCategories %}
    <li>{{ category.name }}</li>
  {% endfor %}
</ul>
```

{% endcode %}

{% code title="Output" %}

```html
<ul>
    <li>Acessórios</li>
    <li>Calçados</li>
    <li>Outlet</li>
    <li>Roupas</li>    
</ul>
```

{% endcode %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://partners.tray.com.br/themes/construindo-seu-template/referencias/funcoes/categories.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
