# How to create a dynamic menu in Jekyll that auto-populates nav items when creating new pages

**URL:** <https://talk.jekyllrb.com/t/how-to-create-a-dynamic-menu-in-jekyll-that-auto-populates-nav-items-when-creating-new-pages/2141>\
**Category:** Help\
**Created:** [September 6, 2018, 2:58pm UTC](https://talk.jekyllrb.com/t/how-to-create-a-dynamic-menu-in-jekyll-that-auto-populates-nav-items-when-creating-new-pages/2141 "2018-09-06T14:58:20Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![nicholastillman](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/nicholastillman/32/1272_2.png) [@nicholastillman](https://talk.jekyllrb.com/u/nicholastillman)\
**Post date:** [September 6, 2018, 2:58pm UTC](https://talk.jekyllrb.com/t/how-to-create-a-dynamic-menu-in-jekyll-that-auto-populates-nav-items-when-creating-new-pages/2141/1 "2018-09-06T14:58:20Z")

</div>

I am wondering how to go about creating a dynamic menu in Jekyll that automatically populates the navigation with nav items whenever a new page is created to make it easier for clients to maintain. I read an article -[https://thinkshout.com/blog/2014/12/creating-dynamic-menus-in-jekyll/](https://thinkshout.com/blog/2014/12/creating-dynamic-menus-in-jekyll/) that touched a bit on the subject, but it was geared toward just sub-nav items. Has anyone had any experience in doing something like this?

Thanks!

---

<div class="post-metadata">

**Author:** ![mmistakes](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/mmistakes/32/2169_2.png) [@mmistakes](https://talk.jekyllrb.com/u/mmistakes)\
**Post date:** [September 6, 2018, 7:34pm UTC](https://talk.jekyllrb.com/t/how-to-create-a-dynamic-menu-in-jekyll-that-auto-populates-nav-items-when-creating-new-pages/2141/2 "2018-09-06T19:34:30Z")

</div>

You can loop through `site.pages` or `site.posts` to populate your nav logic. I believe Jekyll’s default theme [Minima](https://github.com/jekyll/minima/blob/master/_includes/header.html) does this to list all of the site’s pages in the header.

---

<div class="post-metadata">

**Author:** ![jhvanderschee](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/jhvanderschee/32/583_2.png) [@jhvanderschee](https://talk.jekyllrb.com/u/jhvanderschee)\
**Post date:** [September 6, 2018, 8:20pm UTC](https://talk.jekyllrb.com/t/how-to-create-a-dynamic-menu-in-jekyll-that-auto-populates-nav-items-when-creating-new-pages/2141/3 "2018-09-06T20:20:24Z")

</div>

It is a bad idea to do this automatically. However, it is very easy to achieve. Here is the code:

```auto
<ul>
  {% for item in site.pages %}
    <li {% if page.url contains item.url %}class="active"{% endif %}><a href="{{ item.url }}">{{ item.title }}</a></li>
  {% endfor %}
</ul>

```

Source: [https://jekyllcodex.org/without-plugin/simple-menu/](https://jekyllcodex.org/without-plugin/simple-menu/)

To define the order of appearence, you might want to add a front matter variable called ‘order’ to the pages and add a different pagenumber to this variable on each page. The code should sort the pages before looping over them. That looks like this:

```auto
{% assign sitepages = site.pages | sort: order %}
{% for item in site.pages %}
...
{% endfor %}

```

Happy coding!

---

<div class="post-metadata">

**Author:** ![nicholastillman](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/nicholastillman/32/1272_2.png) [@nicholastillman](https://talk.jekyllrb.com/u/nicholastillman)\
**Post date:** [September 13, 2018, 1:49pm UTC](https://talk.jekyllrb.com/t/how-to-create-a-dynamic-menu-in-jekyll-that-auto-populates-nav-items-when-creating-new-pages/2141/4 "2018-09-13T13:49:59Z")

</div>

Thanks a bunch for help and link. Also, why would it be a bad idea, just curious?

Thanks again!

---

<div class="post-metadata">

**Author:** ![jhvanderschee](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/jhvanderschee/32/583_2.png) [@jhvanderschee](https://talk.jekyllrb.com/u/jhvanderschee)\
**Post date:** [September 27, 2018, 12:39pm UTC](https://talk.jekyllrb.com/t/how-to-create-a-dynamic-menu-in-jekyll-that-auto-populates-nav-items-when-creating-new-pages/2141/5 "2018-09-27T12:39:34Z")

</div>

It is a bad idea since this script does not allow you to make unlisted pages. Adding pages to your menu is not a regular thing you want to do, I guess, while creating pages (hopefully) is.

---

<div class="post-metadata">

**Author:** ![Vadorequest](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/vadorequest/32/2383_2.png) [@Vadorequest](https://talk.jekyllrb.com/u/Vadorequest)\
**Post date:** [March 26, 2020, 4:09pm UTC](https://talk.jekyllrb.com/t/how-to-create-a-dynamic-menu-in-jekyll-that-auto-populates-nav-items-when-creating-new-pages/2141/6 "2020-03-26T16:09:43Z")

</div>

[https://pmarsceill.github.io/just-the-docs/](https://pmarsceill.github.io/just-the-docs/) looks like a really good theme for github documentation.

---

<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:** [March 26, 2020, 9:09pm UTC](https://talk.jekyllrb.com/t/how-to-create-a-dynamic-menu-in-jekyll-that-auto-populates-nav-items-when-creating-new-pages/2141/7 "2020-03-26T21:09:45Z")

</div>

Indeed as mentioned before minima uses approach to iterate over pages (I think alphabetically)

- [https://github.com/jekyll/minima/blob/master/\_includes/header.html](https://github.com/jekyll/minima/blob/master/_includes/header.html)

If you at the top you’ll see that it uses `header_pages` as a list set if config and then falls back to pages it can find on its own.

---

<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:** [March 26, 2020, 9:12pm UTC](https://talk.jekyllrb.com/t/how-to-create-a-dynamic-menu-in-jekyll-that-auto-populates-nav-items-when-creating-new-pages/2141/8 "2020-03-26T21:12:49Z")

</div>

Also for interest see the jekyll menus plugin. I haven’t tried it but allows nesting of menus and setting of weights and it looks like either frontmatter or a data file.

- [https://github.com/forestryio/jekyll-menus/blob/master/README.md](https://github.com/forestryio/jekyll-menus/blob/master/README.md)
