【问题标题】:Piranha CMS 4.x Custom Blocks for listing blog content用于列出博客内容的 Piranha CMS 4.x 自定义块
【发布时间】:2019-01-30 01:59:19
【问题描述】:

我使用 Piranha CMS 4.x(核心 5.3.0)已有一段时间了。很整洁。

我正在尝试创建一个“自定义块”,按 slug 或 id 列出特定页面的最新博客文章。有人成功了吗?

我不是高级 dotnet 玩家。

【问题讨论】:

    标签: asp.net .net content-management-system piranha-cms


    【解决方案1】:

    您可以创建一个使用PageField 的块,让您在站点地图中选择一个页面。给定您的块,用户应该选择ArchivePage,这就是将从中加载帖子的位置。您还可以添加一个字段来指定要显示的帖子数。

    ArchiveBlock.cs

    using System;
    using Piranha;
    using Piranha.Extend;
    using Piranha.Extend.Fields;
    using Piranha.Models;
    
    namespace RazorWeb.Models.Blocks
    {
        [BlockType(Name = "Archive", Category = "Content", Icon = "fas fa-pause")]
        public class ArchiveBlock : Block
        {
            public PageField ArchivePage { get; set; }
            public NumberField NumPosts { get; set; }
    
            public T GetArchive<T>(IApi api) where T : ArchivePage<T>
            {
                if (ArchivePage.HasValue)
                {
                    return api.Archives.GetById<T>(ArchivePage.Id.Value, 1, null, null, NumPosts.Value.HasValue ? NumPosts.Value : 3);
                }
                return null;
            }
        }
    }
    

    ArchiveBlock.cshtml

    @model RazorWeb.Models.Blocks.ArchiveBlock
    
    <div class="form-group">
        <label>Select Archive</label>
        @Html.EditorFor(m => m.ArchivePage)
    </div>
    <div class="form-group">
        <label>Set the maximum number of posts</label>
        @Html.EditorFor(m => m.NumPosts)
    </div>
    

    在网络中的 DisplayTemplate 中是这样的(更改以适合您的布局)

    ArchiveBlock.cshtml

    @model RazorWeb.Models.Blocks.ArchiveBlock
    @{
        var archive = Model.GetArchive<RazorWeb.Models.BlogArchive>(WebApp.Api);
    }
    
    @foreach (var post in archive.Archive.Posts)
    {
        <a href="@post.Permalink"><h1>@post.Title</h1></a>
    }
    

    最好的问候

    【讨论】:

      猜你喜欢
      • 2014-11-02
      • 2021-03-06
      • 2015-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多