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

房东的猫 提交于 2019-12-01 02:46:38

问题


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.


回答1:


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.




回答2:


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'),
)



回答3:


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



回答4:


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.




回答5:


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.



来源:https://stackoverflow.com/questions/18520046/how-can-i-control-the-order-of-pages-from-within-a-pelican-article-category

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!