# Link

The link object cannot be called on its own.

The `link` object cannot be called on its own. It must be called inside a [linklist](https://developer.versacommerce.de/articles/liquid-objects-linklist).

The `link` object has the following attributes:

## link.active

Returns `true` if the link object is active, `false` if it is inactive.

A link is active if the resource it points to is part of the URL structure. For example, if the current page is `/blogs/news/snowfall`, these links are active:

- `/blogs/news`, where the resource is the blog
- `/blogs/news/snowfall`, where the resource is the blog article

Because of this behavior, several links in a menu can have `link.active` set to `true` at the same time, even if their `link.url` values differ.

`link.active` is useful for menu designs that highlight top-level navigation categories, for example a menu that highlights the link to the "News" blog while an article of that blog is being read.

## More examples

On a collection page filtered by tags, a link to the unfiltered collection page still returns `true` for `link.active`. For example, if the URL is `collections/men/boots`, these links are active:

- `collections/men`, where the resource is the collection
- `collections/men/boots`, where the resource is the filtered collection

On a [collection-aware](https://developer.versacommerce.de/articles/liquid-filters-url-filters#within) product page, `link.active` returns `true` for the collection-aware product URL and for the collection-independent URL. For example, if the URL is `/products/awesome-product`, these links are active:

- `/products/awesome-product`
- `/collections/awesome-collection/products/awesome-product`
- `/collections/all/products/awesome-product`

## link.child_active

Like [`link.active`](#link-active), but returns `true` if a child link of the parent link is active, `false` if no child links are active.

This example shows the Liquid code and output when the page "Luebeck" is viewed. The example site has this main menu structure:

```text
Main menu
  └ Home
  └ About us
    └ Locations
      └ Luebeck
      └ Hannover
```

```liquid
{{ linklists.main.title }}

{% for link in linklists.main.links %}
  {{ link.title }}: child_active: {{ link.child_active }}
  {% for sub_link in link.links %}
    {{ sub_link.title }}: child_active: {{ sub_link.child_active }}
    {% for sub_sub_link in sub_link.links %}
      {{ sub_sub_link.title }}: active: {{ sub_sub_link.active }}
    {% endfor %}
  {% endfor %}
{% endfor %}
```

```text
Main menu
Home: child_active: false
About us: child_active: true
  In the news: child_active: false
  Locations: child_active: true
    Luebeck: active: true
    Hannover: active: false
```

## link.current

Returns `true` if the page content of the `link` is considered to match the current page, `false` otherwise.

This example shows the Liquid code and output when the page `/collections/winter` is viewed. The example site has a [linklist](https://developer.versacommerce.de/articles/liquid-objects-linklist) with these three links:

- Summer collection, `/collections/summer`
- Winter collection, `/collections/winter`
- All products, `/collections/all`

```liquid
{% for link in linklists.main.links %}
  {{ link.url }}
{% endfor %}
```

```html
/collections/summer
/collections/winter
/collections/all
```

`link.current` does not look for exact matches between the current page URL and a [`link.url`](#link-url) value. There are two important behaviors to keep in mind:

- `link.current` ignores URL parameters
- `link.current` treats product URLs and [collection-aware](https://developer.versacommerce.de/articles/liquid-filters-url-filters#within) product URLs as matching page content

## Ignoring URL parameters

If the current page URL is `/collections/winter?sort_by=price_ascending`, `link.current` is `true` for a link with a `link.url` of `/collections/winter`.

This is useful because a collection link highlighted in a menu should stay highlighted after the collection is re-sorted.

## Handling collection-aware product URLs

If the current page URL is `/products/wool-gloves`, `link.current` returns `true` for these `link` objects:

- `/collections/gloves/products/wool-gloves`
- `/products/wool-gloves`

This helps to reliably detect when the product being viewed is in a menu. The product page content is usually the same whether or not the link is [collection-aware](https://developer.versacommerce.de/articles/liquid-filters-url-filters#within).

## link.child_current

Returns `true` if a child link has a `link` object with [`link.current`](#link-current) equal to `true`. Returns `false` if no child link does.

This example shows the Liquid code and output when the page "Luebeck" is viewed. The example site has this main menu structure:

```text
Main menu
  └ Home
  └ About us
    └ Locations
      └ Luebeck
      └ Hannover
```

```liquid
{{ linklists.main.title }}

{% for link in linklists.main.links %}
  {{ link.title }}: child_current: {{ link.child_current }}
  {% for sub_link in link.links %}
    {{ sub_link.title }}: child_current: {{ sub_link.child_current }}
    {% for sub_sub_link in sub_link.links %}
      {{ sub_sub_link.title }}: current: {{ sub_sub_link.current }}
    {% endfor %}
  {% endfor %}
{% endfor %}
```

```text
Main menu
Home: child_current: false
About us: child_current: true
  In the news: child_current: false
  Locations: child_current: true
    Luebeck: current: true
    Hannover: current: false
```

## link.levels

Returns the number of nested levels a link contains.

Take this main menu structure:

```text
Main menu
  └ Home
  └ About us
    └ Locations
      └ Luebeck
      └ Hannover
```

This Liquid code:

```liquid
{% assign menu = linklists.main-menu %}

{{ menu.title }}: {{ menu.levels }}
{% for link in menu.links %}
  {{ link.title }}: {{ link.levels }}
  {% for sub_link in link.links %}
    {{ sub_link.title }}: {{ sub_link.levels }}
    {% for sub_sub_link in sub_link.links %}
      {{ sub_sub_link.title }}: {{ sub_sub_link.levels }}
    {% endfor %}
  {% endfor %}
{% endfor %}
```

returns this output (edited for space):

```text
Main menu: 3
  Home: 0
  About us: 2
    Locations: 1
      Luebeck: 0
      Hannover: 0
```

## link.links

Returns an array of the child links of the parent link.

## link.object

Returns the variable associated with the link. Possible types:

- product
- collection
- page
- blog

With `link.object` you can access all attributes of these variables.

```liquid
<!-- If the link points to a product with a price of €10 -->
{{ link.object.price | money }}
```

```text
€10
```

## link.title

Returns the title of the link.

## link.type

Returns the type of the link. Possible values:

- **blog_link**: the link points to a blog
- **catalog_link**: the link points to the catalog page `/collections/all`
- **collection_link**: the link points to a collection
- **collections_link**: the link points to the collection list page `/collections`
- **frontpage_link**: the link points to the home page `/`
- **http_link**: the link points to an external page or to a type or vendor collection (for example `/collections/types?q=Pants`)
- **page_link**: the link points to a page
- **policy_link**: the link points to a policy page `/policies`
- **product_link**: the link points to a product page
- **search_link**: the link points to the search page `/search`

## link.url

Returns the URL of the link.
