ERPNext网页模板:如何展示特定分类的博客文章
如何在ERPNext Page Builder区块中仅展示特定分类的博客文章
要实现仅展示特定分类的博客文章,你需要修改模板中frappe.get_all的查询逻辑,添加分类过滤条件,以下是两种可行方案:
方案1:硬编码指定分类(快速实现)
直接在模板中指定要展示的分类名称,修改后的代码如下:
<section class="container my-5"> <h2 class="section-title">{{ _('जलक्रांतीच्या गाथा') }} </h2> <p class="section-description">{{ _('ब्लॉगच्या माध्यमातुन') }} </p> <div class="row"> <!-- 替换成你需要展示的博客分类名称,也可以用分类ID更稳定 --> {% set target_category = "你的目标分类名称" %} {% for post in frappe.get_all("Blog Post", fields=["title", "blogger", "blog_intro", "route", "meta_image"], filters=[["Blog Post Category", "blog_category", "=", target_category]], order_by="published_on desc", limit=6, distinct=True ) %} <div class="col-md-4 mb-4"> <div class="card h-100"> <img src={{ post.meta_image }} alt="" class="image-wiih-blur card-img-top image-loaded" max-width="400" max-height="400"> <div class="card-body"> <h5 class="card-title">{{ post.title }}</h5> <p class="card-subtitle mb-2 text-muted">{{ _('By {0}').format(post.blogger) }}</p> <p class="card-text">{{ post.blog_intro }}</p> </div> <div class="card-body flex-grow-0"> <a href="{{ post.route }}" class="card-link">{{ _('पुढे वाचा') }}</a> </div> </div> </div> {% endfor %} </div> </section>
distinct=True:避免同一篇文章属于多个分类时重复显示- 优先使用分类ID代替名称,防止分类名称修改后过滤失效
方案2:支持Page Builder选择分类(灵活复用)
如果需要在添加区块时自由选择分类,需要先添加自定义字段,再修改模板:
步骤1:添加自定义字段
- 进入ERPNext后台,打开自定义字段页面
- 为
Web Page Blockdoctype添加新字段:- 字段类型:链接
- 链接到:博客分类
- 字段名称:
blog_category - 标签:博客分类
- 允许为空:是(留空时显示所有文章)
步骤2:修改模板代码
<section class="container my-5"> <h2 class="section-title">{{ _('जलक्रांतीच्या गाथा') }} </h2> <p class="section-description">{{ _('ब्लॉगच्या माध्यमातुन') }} </p> <div class="row"> {% set filters = [] %} <!-- 如果区块配置了分类,添加过滤条件 --> {% if doc.blog_category %} {% do filters.append(["Blog Post Category", "blog_category", "=", doc.blog_category]) %} {% endif %} {% for post in frappe.get_all("Blog Post", fields=["title", "blogger", "blog_intro", "route", "meta_image"], filters=filters, order_by="published_on desc", limit=6, distinct=True ) %} <div class="col-md-4 mb-4"> <div class="card h-100"> <img src={{ post.meta_image }} alt="" class="image-wiih-blur card-img-top image-loaded" max-width="400" max-height="400"> <div class="card-body"> <h5 class="card-title">{{ post.title }}</h5> <p class="card-subtitle mb-2 text-muted">{{ _('By {0}').format(post.blogger) }}</p> <p class="card-text">{{ post.blog_intro }}</p> </div> <div class="card-body flex-grow-0"> <a href="{{ post.route }}" class="card-link">{{ _('पुढे वाचा') }}</a> </div> </div> </div> {% endfor %} </div> </section>
修改后,在Page Builder添加该区块时,就能直接选择要展示的博客分类,无需每次修改代码。
内容的提问来源于stack exchange,提问作者learnig-harsh
相关产品推荐
相关产品推荐

