Baham's Blog

Above all else, guard your heart.

Add Descritpion and Keywords to Octopress Site Plugin Version 3

In version 2 plugin, the code is :

(add_keywords_and_description_new.rb) download
1
2
3
4
5
{% capture description %}{% if page.description %}{{ page.description }}{% elsif page.title %}{{ content | raw_content }}{% else %}{{ site.description }}{% endif %}{% endcapture %}
<meta name="description" content="{{ description | strip_html | condense_spaces | truncate:150 }}" />

{% capture keywords %}{% if page.keywords %}{{ page.keywords }}{% elsif page.title %}{{""}}{% else %}{{ site.keywords }}{% endif %}{% endcapture %}
<meta name="keywords" content="{{ keywords | strip_html | condense_spaces }}" />

And after change the head.html to that, the site’s homepage will show site’s description and keywords, and post show the post’s, if the post has not, it will show no keywords but part of content as description, and the page is like the post.

Besides, categories link will show description like Category: Name -Baham's Blog, the archive has its own description and keywords. But yesterday I found I ignore one thing, the pages with number. t means /blog/page/bumber these pages still show the same description and keywords as site’s.

So I need to change some codes.

(add_keywords_and_description_version3.rb) download
1
2
3
4
5
6
7
8
9
10
<head>
  <meta charset="utf-8">
  <title>{% if page.title %}{{ page.title }}-{% elsif page.url contains "blog/page" %}{{ 'One of the Pages From' }}-{% endif %}  {{ site.title }}</title>
  <meta name="author" content="{{ site.author }}">

{% capture description %}{% if page.description %}{{ page.description }}{% elsif page.title %}{{ content | raw_content }}{% elsif page.url contains"blog/page" %}{{ "" }}{% else %}{{ site.description  }}{% endif %}{% endcapture %}
<meta name="description" content="{{ description | strip_html | condense_spaces | truncate:150 }}" />

{% capture keywords %}{% if page.keywords %}{{ page.keywords }}{% elsif page.title %}{{""}}{% elsif page.url contains "blog/page" %}{{ "" }}{% else %}{{ site.keywords }}{% endif %}{% endcapture %}
<meta name="keywords" content="{{ keywords | strip_html | condense_spaces }}" />

Release Notes

  1. Make pages show different description and keywords with homepage. The keywords and description will show nothing;
  2. Make pages show different titles with home page. The pages will show One of the pages from -Baham's Blog.
  3. pages mean /blog/page/number

Version 2 Notes

  1. Make every links show the description different with the homepage;(except /blog/page/number)
  2. Make links without description show part of content as description;
  3. The keywords of the links without keywords will keep space.

Original Version

PS:

  1. Finding the codes’ bugs really make me tired.
  2. You can still use version 2’s code, and just change the robots.txt to disallow index /blog/page/
1
Disallow: /blog/page/




The Original Link: http://baham.github.io/05_08_add-descritpion-and-keywords-for-octopress-site-plugin-v3.html
If you want to reprint it, please do under the CC BY-NC-SA 4.0

Comments