"tag" 和 "categories" 页面的元描述

meta-description for "tag" and "categories" pages

对于 HEXO 博客...我知道元描述写在配置文件中并且适用于所有页面。

对于博文,我可以创建单独的元描述,这适用于搜索引擎。

但是,我的 "tag" 和 "categories" 页面现在已编入索引并带有主页的元描述。 情况不妙。

所以我问是否可以为 "tag" 和 "categories" 页面创建自定义元描述? 就像是... description: this is a page about {{tag}}

description: this is a page about {{category}}

这是我 head.ejs 中的代码。 站点主配置文件有 description: main config meta-description text.

<%if(metaDescription){%>
<meta name="description" content="<%= config.description %>">
    <% } else if (page.description){ %>
      <meta name="description" content="<%= page.description %>">
      <% } else if (page.excerpt){ %>
      <meta name="description" content="<%= strip_html(page.excerpt).replace(/^\s*/, '').replace(/\s*$/, '').replace(/[\n,\r]/g,'') %>">
      <% } else if (page.content){ %>
      <meta name="description" content="<%= strip_html(page.content).replace(/^\s*/, '').replace(/\s*$/, '').substring(0, 150).replace(/[\n,\r]/g,'') %>">
      <% } else if (config.description){ %>
      <meta name="description" content="<%= config.description %>">
      <% } 
      <% if (page.keywords){ %>
        <meta name="keywords" content="<%= page.keywords %>">
      <% } else if (page.tags){ %>
      <%
        var thistags=[];
        page.tags.each(function(k){ 
        thistags.push(k.name);
        }) %>
        <meta name="keywords" content="<%= thistags %>">
      <% } %>

这一切都由您的主题处理,因此您需要在其中添加逻辑。

您需要检查您的页面是标签还是类别索引,并在那里生成自定义描述:

let description = page.description; // Normal case when your desc comes from meta data


else if (page.tag) {
    description = 'This is a page about ' + page.tag;
} else if (page.category) {
    description = 'This is a page about ' + page.category;
}

// Use your description variable as you are currently doing

编辑:基于 post 更新(添加 head.ejs

else if (page.tag) {
  <meta name="description" content="<%= 'This is a page about ' + page.tag %>">
} else if (page.category) {
    <meta name="description" content="<%= 'This is a page about ' + page.category %>">
}