Skip to content

Commit

Permalink
Related pages for posts
Browse files Browse the repository at this point in the history
  • Loading branch information
alanta committed Nov 19, 2020
1 parent 2b84845 commit 99f10ef
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 63 deletions.
4 changes: 4 additions & 0 deletions Models/ContentTypes/Page.Generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public partial class Page
{
public const string Codename = "page";
public const string BodyCodename = "body";
public const string CategoriesCodename = "categories";
public const string ImageCodename = "image";
public const string MetadataMetaDescriptionCodename = "metadata__meta_description";
public const string MetadataMetaKeywordsCodename = "metadata__meta_keywords";
Expand All @@ -27,10 +28,12 @@ public partial class Page
public const string MetadataTwitterSiteCodename = "metadata__twitter_site";
public const string RelatedCodename = "related";
public const string SettingsCodename = "settings";
public const string TagsCodename = "tags";
public const string TitleCodename = "title";
public const string UrlSlugCodename = "url_slug";

public string Body { get; set; }
public IEnumerable<ITaxonomyTerm> Categories { get; set; }
public IEnumerable<IAsset> Image { get; set; }
public string MetadataMetaDescription { get; set; }
public string MetadataMetaKeywords { get; set; }
Expand All @@ -45,6 +48,7 @@ public partial class Page
public IEnumerable<object> Related { get; set; }
public IEnumerable<IMultipleChoiceOption> Settings { get; set; }
public IContentItemSystemAttributes System { get; set; }
public IEnumerable<ITaxonomyTerm> Tags { get; set; }
public string Title { get; set; }
public string UrlSlug { get; set; }
}
Expand Down
2 changes: 2 additions & 0 deletions Models/ContentTypes/Post.Generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public partial class Post
public const string Codename = "post";
public const string AuthorCodename = "author";
public const string BodyCodename = "body";
public const string CategoriesCodename = "categories";
public const string ImageCodename = "image";
public const string MetadataMetaDescriptionCodename = "metadata__meta_description";
public const string MetadataMetaKeywordsCodename = "metadata__meta_keywords";
Expand All @@ -36,6 +37,7 @@ public partial class Post

public IEnumerable<object> Author { get; set; }
public string Body { get; set; }
public IEnumerable<ITaxonomyTerm> Categories { get; set; }
public IEnumerable<IAsset> Image { get; set; }
public string MetadataMetaDescription { get; set; }
public string MetadataMetaKeywords { get; set; }
Expand Down
5 changes: 5 additions & 0 deletions Pipelines/Pages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Statiq.Core;
using Statiq.Razor;
using Statiq.SearchIndex;
using System.Linq;

namespace Kentico.Kontent.Statiq.Lumen.Pipelines
{
Expand All @@ -19,6 +20,10 @@ public Pages(IDeliveryClient deliveryClient, SiteSettings site)
new Kontent<Page>(deliveryClient)
.OrderBy(Post.TitleCodename, SortOrder.Descending)
.WithQuery(new DepthParameter(2), new IncludeTotalCountParameter()),
new SetMetadata(nameof(Page.Tags),
KontentConfig.Get<Page,string[]>(post => post.Tags?.Select(t => t.Codename).ToArray())),
new SetMetadata(nameof(Page.Categories),
KontentConfig.Get<Page,string[]>(post => post.Categories?.Select(t => t.Codename).ToArray())),
new SetDestination(KontentConfig.Get((Page page) => new NormalizedPath(page.Url))),
new SetMetadata(SearchIndex.SearchItemKey, Config.FromDocument((doc, ctx) =>
{
Expand Down
2 changes: 2 additions & 0 deletions Pipelines/Posts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public Posts(IDeliveryClient deliveryClient, SiteSettings site)
.WithQuery(new DepthParameter(2), new IncludeTotalCountParameter()),
new SetMetadata(nameof(Post.Tags),
KontentConfig.Get<Post,string[]>(post => post.Tags?.Select(t => t.Codename).ToArray())),
new SetMetadata(nameof(Post.Categories),
KontentConfig.Get<Post,string[]>(post => post.Categories?.Select(t => t.Codename).ToArray())),
new SetDestination(KontentConfig.Get((Post post) => new NormalizedPath(post.Url))),
new SetMetadata(SearchIndex.SearchItemKey, Config.FromDocument((doc,ctx)=>
{
Expand Down
2 changes: 1 addition & 1 deletion appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"DeliveryOptions": {
"ProjectId": "e10d7fbd-315f-01e2-20a5-e067cdf43f2f"
},
"LinkHideExtensions": "true",
"LinkHideExtensions": false,
"Host": "",
"LinksUseHttps": true,
"LinkRoot": "",
Expand Down
4 changes: 2 additions & 2 deletions input/Shared/postbox.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
<div class="blog-grid-item">
<div class="card h-100">
<div class="maxthumb">
<a href="@Model.Url">
<a href="@Html.GetLink(Model.Url)">
@Html.Image(Model.Image, Model.Title, site.LazyImages, 580, 400, ImageFitMode.Crop, new { @class = "img-thumb" })
</a>
</div>
<div class="card-body">
<h2 class="card-title">
<a class="text-dark" href="@Model.Url">@Model.Title</a>
<a class="text-dark" href="@Html.GetLink(Model.Url)">@Model.Title</a>
@if (Model.Rating.HasValue)
{
<div class="mb-2 mt-2 font-weight-normal">
Expand Down
39 changes: 39 additions & 0 deletions input/Shared/related-posts.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
@using Kentico.Kontent.Statiq.Memoirs.Models
@model Kentico.Kontent.Statiq.Memoirs.Models.Post
<!-- Related Posts
================================================== -->
@if (!Model.Tags.Any() && !Model.Categories.Any() && !Model.Related.Any())
{
return;
}

<div class="related-posts">

<h2 class="text-center mb-4">Explore more like this</h2>

<div class="d-flex justify-content-center align-items-center">

<!-- Categories -->
@foreach (var category in Model.Categories.OrderBy(c => c.Name))
{
<a class="smoothscroll badge badge-primary text-capitalize" href="@Html.GetLink($"/categories#{category.Codename}")">@category.Name</a>
}

<!-- Tags -->
@foreach (var tag in Model.Tags.OrderBy(c => c.Name))
{
<a class="smoothscroll badge badge-primary text-capitalize" href="@Html.GetLink($"/tags#{tag.Codename}")">@tag.Name</a>
}

</div>

@if (Model.Related.Any())
{
<div class="blog-grid-container">
@foreach (IPageMetadata related in Model.Related.OfType<IPageMetadata>().Take(3))
{
<partial name="postbox" model="related"/>
}
</div>
}
</div>
54 changes: 0 additions & 54 deletions input/Shared/related-posts.html

This file was deleted.

9 changes: 3 additions & 6 deletions input/post.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ else
<partial name="Shared/_featured-image" model="Model.Image" />

<!-- Content -->
@* {% include article.html %} *@
<div class="article-post">
@Html.Raw(Model.Body)
</div>
<partial name="Shared/_article" model="Model"/>

<!-- Rating -->
@if (Model.Rating.HasValue)
Expand All @@ -46,13 +43,13 @@ else
<partial name="Shared/author-box" model="author" />

<!-- Comments if not disabled with comments: false -->
<partial name="comments" model="Model"/>
<partial name="Shared/comments" model="Model"/>

<!-- Share -->
<partial name="Shared/share" model="Model" />

<!-- Related Post -->
{% include related-posts.html %}
<partial name="Shared/related-posts" model="Model" />

<!-- Review with LD-JSON, adapt it for your needs if you like, but make sure you test the generated HTML source code first:
https://search.google.com/structured-data/testing-tool/u/0/
Expand Down

0 comments on commit 99f10ef

Please sign in to comment.