Search code examples
metadatameta-tagshexo

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


For HEXO blog... I know that meta-description is written in config file and works across all pages.

For blogposts I can create individual meta-descriptions and that works for the search engines.

However, my "tag" and "categories" pages get indexed now and with meta-description of home-page. This is not good.

So I am asking if it is possible to create a custom meta-description for "tag" and "categories" pages? Something like... description: this is a page about {{tag}}

and

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

This is the code in my head.ejs. The site main config file has 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 %>">
      <% } %>


Solution

  • This is all handled by your theme so you need to add the logic there.

    You need to check if your page is a tag or category index and generate a custom description there:

    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
    

    EDIT: based on post update (added 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 %>">
    }