Querying for specific articles (via tag/category) in Pelican themes

末鹿安然 提交于 2019-12-22 09:00:03

问题


Is it possible to set query parameters via jinja in Pelican template files?

index.html

{% if articles %}
    {% for article in articles_page.object_list if article.category == 'article' %}        
        #stuff
    {% endfor %}
{% endif %}

This will return articles in the article category, but only if they happen to be in the articles already queried for. The desirable setup would be to grab x articles in y category (or with y tag) - is that possible?


回答1:


This code snippet works for me to bring back a list of all articles matching a tag:

 {% block content %}
 <ul>
 {% for article in articles if FAVORITES_TAG in article.tags %}
   {% if loop.index <= FAVORITES_COUNT %} 
     <li><a href="{{ SITEURL }}/{{ article.url }}">{{ article.title }}</a></li>
   {% endif %}
 {% endfor %}
</ul>
{% endblock %}

I set the FAVORITES_TAG and FAVORITES_COUNT variables in the config. I hope that helps.




回答2:


I ran into the same problem, and found a solution

{% if articles %}
    {% for article in articles_page.object_list if article.category.name == 'article' %}        
        #stuff
    {% endfor %}
{% endif %}


来源:https://stackoverflow.com/questions/19283880/querying-for-specific-articles-via-tag-category-in-pelican-themes

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