如何按类别按类别获取帖子?

时间:2017-12-20 09:11:55

标签: javascript hexo

乱搞Hexo博客..我有一个类别页面,列出了每个类别中的所有帖子。但我想要一个类别独有的单独页面。我不熟悉这里的语法。如何筛选名为“精选”的类别?

<div class="archives-wrap" style="margin: 0px;">
 <div class="archives-category-wrap">
    <blockquote>
    <% if(site.categories.length) { %>
      <%- list_categories(site.categories) %>
    <% } %>
    </blockquote>
</div>

<% site.categories.sort('name').map(function(category){  %>
<div class="archives-wrap">
    <div class="archive-year-wrap" id="<%= category.name %>">
        <h1 class="archive-category"><%= category.name %></h1>
    </div>
    <div class="archives">
        <% category.posts.sort('-date').map(function(post, i){  %>
            <%- partial('_partial/archive-post', {post: post, index: true}) %>
            <% if (post.subtitle && post.subtitle.length) { %>
                <h3 class="post-subtitle">
                    <%- post.subtitle %>
                </h3>
            <% } %>
        <% }) %>
    </div>
</div>
<% }) %>

1 个答案:

答案 0 :(得分:1)

我也不熟悉Hexo,但我认为一个简单的IF语句可以完成这项任务:

<div class="archives-wrap" style="margin: 0px;">
 <div class="archives-category-wrap">
    <blockquote>
    <% if(site.categories.length) { %>
      <%- list_categories(site.categories) %>
    <% } %>
    </blockquote>
</div>

<% site.categories.sort('name').map(function(category){  %>
    <% if(category.name == 'featured') { %>
        <div class="archives-wrap">
            <div class="archive-year-wrap" id="<%= category.name %>">
                <h1 class="archive-category"><%= category.name %></h1>
            </div>
            <div class="archives">
                <% category.posts.sort('-date').map(function(post, i){  %>
                    <%- partial('_partial/archive-post', {post: post, index: true}) %>
                    <% if (post.subtitle && post.subtitle.length) { %>
                        <h3 class="post-subtitle">
                            <%- post.subtitle %>
                        </h3>
                    <% } %>
                <% }) %>
            </div>
        </div>
    <% } %>
<% }) %>

更精细的解决方案可能是在map之前使用过滤器功能,但我在他们的文档中没有找到这样的功能。

相关问题