# Best way to target a specific page in a collection

**URL:** <https://talk.jekyllrb.com/t/best-way-to-target-a-specific-page-in-a-collection/5958>\
**Category:** Uncategorized\
**Created:** [April 28, 2021, 5:37am UTC](https://talk.jekyllrb.com/t/best-way-to-target-a-specific-page-in-a-collection/5958 "2021-04-28T05:37:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![vsiege](https://avatars.discourse-cdn.com/v4/letter/v/db5fbb/32.png) [@vsiege](https://talk.jekyllrb.com/u/vsiege)\
**Post date:** [April 28, 2021, 5:37am UTC](https://talk.jekyllrb.com/t/best-way-to-target-a-specific-page-in-a-collection/5958/1 "2021-04-28T05:37:16Z")

</div>

Rather than loop through my collection ‘products’ then use a condition to check if it’s the specific page I want… is there a way to to target a specific page?

Here is the page I would like to target in liquid

```
_collections
  _products
      sample
         index.md

```

Thanks for your helps

---

<div class="post-metadata">

**Author:** ![BillRaymond](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/billraymond/32/5963_2.png) [@BillRaymond](https://talk.jekyllrb.com/u/BillRaymond)\
**Post date:** [April 28, 2021, 7:45pm UTC](https://talk.jekyllrb.com/t/best-way-to-target-a-specific-page-in-a-collection/5958/2 "2021-04-28T19:45:46Z")

</div>

If you can use something from the front matter, that would be ideal. For example, if you have a collection item that looks like this:

```auto
---
title: Sample
---
Content to display for the collection item...

```

You can then create code to display the item with code like this:

```auto
<h2>Here is a sample</h2>
{%- assign samples = site.samples | where: 'title', 'Sample' -%}
{% for sample in samples %}
    <p>Title: {{ sample.title }}</p>
{% endfor %}

```

By assigning the variable first, you can add a `where` clause and you should get the results you are looking for.

---

<div class="post-metadata">

**Author:** ![vsiege](https://avatars.discourse-cdn.com/v4/letter/v/db5fbb/32.png) [@vsiege](https://talk.jekyllrb.com/u/vsiege)\
**Post date:** [April 29, 2021, 4:57am UTC](https://talk.jekyllrb.com/t/best-way-to-target-a-specific-page-in-a-collection/5958/3 "2021-04-29T04:57:58Z")

</div>

This looks simple enough to implement. Thank you @BillRaymond !

---

<div class="post-metadata">

**Author:** ![MichaelCurrin](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/michaelcurrin/32/2340_2.png) [@MichaelCurrin](https://talk.jekyllrb.com/u/MichaelCurrin)\
**Post date:** [April 29, 2021, 11:44am UTC](https://talk.jekyllrb.com/t/best-way-to-target-a-specific-page-in-a-collection/5958/4 "2021-04-29T11:44:25Z")

</div>

## where filter

Indeed you need a where clause to get a page. By name, title, whatever.

A for loop is unnecessary if you will get most one result.

```auto
{%- assign sample = site.samples | where: 'title', 'Sample' | first-%}

```

* * *

## link tag

I would say there is a more appropriate way to get a single page on Jekyll site.

Use the `link` tag. Pass it the full path to a page, not just the filename. So it is accurate.

If the path is invalid because you made a typo or the page is renamed or moved, you’ll find out when you run Jekyll serve or build. Whereas using `where` means you might get 0, 1, or even more pages and if you get zero then you’ll deploy a page which is missing section because it could not find the page it was expecting.

Say for an HTML `a` tag, here are examples

```auto
href="{% link index.md %}"

href="{% link team/joe.md %}"

href="{% link _products/appliances/coffee-machine.md %}"

```

Note that the path is relative to repo root, regardless of the current page.

If using Jekyll 3 then you need a subpath:

```auto
href="{{ site.baseurl }}{% link index.md %}"

```

---

<div class="post-metadata">

**Author:** ![MichaelCurrin](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/michaelcurrin/32/2340_2.png) [@MichaelCurrin](https://talk.jekyllrb.com/u/MichaelCurrin)\
**Post date:** [April 29, 2021, 11:44am UTC](https://talk.jekyllrb.com/t/best-way-to-target-a-specific-page-in-a-collection/5958/5 "2021-04-29T11:44:35Z")

</div>

Also note that your structure is not typical.

```auto
_collections
  _products

```

A collection is usually

```auto
_products

```

And setup in config adds `products` (no underscore) under collections variable.

Then access a product pages with

```auto
site.pages # all pages. Including posts, plain pages and collections 

site.products # all pages in the products collection 

site.collections # includes multiple collections 

```

I have a simple collection demo here as languages. I iterate through pages in the collection.

> **[MichaelCurrin/jekyll-blog-demo](https://github.com/MichaelCurrin/jekyll-blog-demo)**
>
> Base for a static website blog - built with Jekyll and the default theme
