mabalenk
mabalenk

Reputation: 1043

Avoid multiple menu entries with Jekyll pagination

I'm using Jekyll paginate plugin to create multiple pages for my blog. To make sure pagination works I have created a separate directory called blog with an index.html file. In the front matter of this index.html file I specify the layout, title and order:

---
layout: default
title:  Blog
order:  8
---

The title value is picked up by Jekyll and used to create a menu entry called "Blog" on the main page of my website. However, once pagination is enabled and all the posts are split into multiple pages, each page will have a title "Blog". This will result in multiple menu entries "Blog". I would like to have only one menu item call "Blog".

I have tried to reset the page title to "empty" for additional pages created by paginator by inserting this code into the index.html:

{% if paginator.page > 1 %}
  {% assign page.title = '' %}
{% endif %}

It seems page.title is not a variable, but an object. Therefore, the assignment was not effective.

Upvotes: 4

Views: 425

Answers (5)

mabalenk
mabalenk

Reputation: 1043

Another elegant solution is to remove duplicate entries while sorting site pages by order. This can be achieved applying Liquid option uniq when the set of sorted pages is created. To do this copy the header.html file from the default Minima installation into your Jekyll _includes directory and introduce the change inside of the <div class="trigger"> environment:

<div class="trigger">
  {% assign sorted_pages = site.pages | uniq:"title" | sort:"order" %}
    {% for my_page in sorted_pages %}
      {% if my_page.title %}
      <a class="page-link" href="{{ my_page.url | relative_url }}">{{ my_page.title | escape }}</a>
      {% endif %}
    {% endfor %}
</div>

Upvotes: 0

VJ_Koushik
VJ_Koushik

Reputation: 53

you could also use unless control flow tag which is the opposite of if like this

{% assign tempPage = "" %}
{% for page in site.pages %}
   {% unless tempPage contains page.title %}
      {% comment %} list out the pages {% endcomment %}
      {{ page.title }}
   {% endunless %}
   {% assign tempPage = page.title | append: tempPage %}
{% endfor %}

here we append all the previously listed page.title to tempPage and the pages will get listed out only if the tempPage does not contain the current page's title in the loop

Upvotes: 1

David Jacquel
David Jacquel

Reputation: 52809

I discriminate on pagination url, which are /blog/, /blog/page1/, .... And I only print pages that doesn't contains 'page'.

{%- for p in pages -%}
  {%- unless p.url contains 'page' -%}
    <li><a href="{{ site.baseurl }}{{ p.url }}">{{ p.title }}</a></li>
  {%- endunless -%}
{% endfor %}

Upvotes: 1

Sylhare
Sylhare

Reputation: 7069

Allo, I don't think you can change the title from the paginator.page.title or from page.title even if you assign it ''.

In my case to fix the issue, I changed the way the title were rendered in the navbar by making sure they were not repeated twice.

    {% assign name_page = "" %}
    {% for page in site.pages %}
    {% if name_page contains page.title %}
    <!-- do nothing if already there -->
    {% else %}
      <li class="separator"> | </li>
      <li>
          <a class="clear" href="{{ page.url | relative_url }}">
            {{ page.title }}
          </a>
      </li>
    {% endif %}
    {% assign name_page = page.title | append: name_page %}
    {% endif %}
    {% endfor %} 

So basically it adds the title to name_page and if the title is there twice it does not print the page title.

Upvotes: 3

Mr. Hugo
Mr. Hugo

Reputation: 12592

How about turning it around, so checking paginator.page < 2 while building your menu? Seems like a more logical solution to me.

Upvotes: 1

Related Questions