How can I control the order of pages from within a pelican article category?
Asked Answered
C

5

12

I am using pelican jinja2 templates in order to generate a navigation menu based on the category and I need a way to control the order of the pages, or at least a trick to allow me to choose the first page to be listed.

{% for a in articles %}
     {% if a.category == category %}
         <li><a href="{{ SITEURL }}/{{ a.slug }}">{{ a.title }}
     {% endif %}
{% endfor %}

How can a make one specific article page to be the first. Their sourse is in markdown format.

Coronation answered 29/8, 2013 at 20:5 Comment(0)
S
16

Pelican 3.5 will introduce built-in support for article and page ordering. You can define in your pelicanconf.py by which metadata attribute articles and pages should be sorted. The two variables are:

ARTICLE_ORDER_BY = 'attribute'
PAGE_ORDER_BY = 'attribute'

For this to work correctly, you must ensure that:

  • every page / article defines the specified attribute, otherwise you will get a compiler error saying the page / article class doesn't have this attribute
  • your attribute always uses the same number of character; so the values 1, 10 and 100 won't produce a correct order, but 001, 010 and 100 will do

With that infrastructure in place, outputting articles in the correct order should work for your code without modifications.

Skylight answered 22/9, 2014 at 20:19 Comment(2)
Do not forget to capitalize attributes 1st letter in pagesTentage
See here in the pelican documentation docs.getpelican.com/en/latest/settings.html#ordering-contentEnrichment
N
9

You can get sorting using Pelican's custom page metadata and Jinja2's built-in sort filter.

Example template:

{% for pg in PAGES|sort(attribute='sortorder') %}
    <li{% if pg == page %} class="active"{% endif %}><a href="{{ SITEURL }}/{{ pg.url }}">{{ pg.title }}</a></li>
{% endfor %}

Example page metadata:

title: User's Manual
date: 2014-06-11 15:11
sortorder: 20
Neuberger answered 11/6, 2014 at 23:19 Comment(4)
Any idea why the sort is not working with Pelican 3.4? With the example above, it says: pelican.contents.Page object has no attribute sortorder. I'm using this kind of code in a page template.Skylight
@Skylight I just tried it with Pelican 3.4 and it seems to work fine. Perhaps one of your pages doesn't have sortorder defined? You can try to debug using this: {% for pg in PAGES %} <!-- {{ pg.__dict__ }} --> {% endfor %}. It should add a comment in the html output with all the attributes on the Page object. sortorder should be one of them.Neuberger
Yes, I also just found it. Each page must have this custom metadata AND each page must use the same number of characters for this attribute. For example, values like 1, 10, 100 won't work, but 001, 010 and 100 work correctly. With Pelican 3.5, they introduce a global setting PAGE_ORDER_BY = 'sortorder', but again you must make sure to have it defined in every page and use always the same length of characters for it.Skylight
one more thing to note here - attribute name in the template should be lowercase (even if the attribute name in your pages is capitalized).Howzell
R
9

To workaround the problem, you can disable categories and pages from being displayed automatically and set the menuitems manually in the configuration:

DISPLAY_CATEGORIES_ON_MENU = False
DISPLAY_PAGES_ON_MENU = False

MENUITEMS = (
    ('Home', '/'),
    ('Archives', '/archives.html'),
    ('Tags', '/tags.html'),
    ('Category1', 'category/category1.html'),
    ('Category2', 'category/category2.html'),
)
Rawley answered 16/7, 2014 at 10:52 Comment(0)
C
0

It seems that at this moment this is not possible yet. There is a feature request and outdated-patch at https://github.com/getpelican/pelican/issues/420

I will update the answer once this is integrated.

Coronation answered 30/8, 2013 at 8:16 Comment(0)
P
0

With pelican 4.0.1 and pelican-bootstrap3 it is simply not possible to sort the menu items for both pages and article categories because they are sorted separately in the base template: first the pages (which are sorted), then the categories (which don't seem to be sorted). So the pages are always placed it front of the categories.

Also, in the template the sorting for the page items are controlled by setting the option PAGES_SORT_ATTRIBUTE, so try setting that one in your pelicanconf.py if the PAGE_ORDER_BY option mentioned in the docs doesn't work.

Bit of a shame, but this answer here by jcollado does seem to solve the problem, how many menu items will you have anyway?

But I had to adjust it a bit:

DISPLAY_CATEGORIES_ON_MENU = False
DISPLAY_PAGES_ON_MENU = False

MENUITEMS = (
    ('Projects', '/category/projects.html'),
    ('Publications', '/pages/Publications.html'),
    ('Music', '/category/music.html'),
    ('About', '/pages/about.html'),
)

Start the URIs with the forward slash, or you might run into some trouble.

Periodicity answered 18/2, 2019 at 2:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.