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

后端 未结 5 913
余生分开走
余生分开走 2021-01-12 14:28

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 a

相关标签:
5条回答
  • 2021-01-12 14:34

    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.

    0 讨论(0)
  • 2021-01-12 14:42

    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'),
    )
    
    0 讨论(0)
  • 2021-01-12 14:48

    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
    
    0 讨论(0)
  • 2021-01-12 14:51

    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.

    0 讨论(0)
  • 2021-01-12 14:53

    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.

    0 讨论(0)
提交回复
热议问题