Baham's Blog

Above all else, guard your heart.

Octopress Add Description Keywords Plugin Optimization

Yesterday, when I just google my own site. I find the results show like this:

The Site's meta description

But I did not want every page without description shows the site’s description.

Cause it is not good to index pages by search engine.

So I want to optimizate the octopress plugin or called code to add the description and keywords.

Old Code

This is the old code.

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

And the article that include the old codes about the octopress seo.

The results I dislike codes bring about are show the page’s description as same as the site’s description.

So I change codes to show part of the page’s content as description instead of site’s description.

Add Description and Keywords new version

(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 }}" />

The code is not a ruby file, please add the codes or change the original code to these after the author meta in source/_include/head.html.

Then the meta will show like :

meta

When you change the codes to these. 1. If the page has a description, it will show the description 2. If has not and this page is a page, it will show the part of page content. 3. Otherwise, show the site’s description, like the homepage.

PS: page means page/post/other links, not just rake new_page's page

And the keywords’ code is like the description’.

Version 3

Optimization about the page with number’s title and description.




The Original Link: http://baham.github.io/05_07_octopress-add-description-keywords-plugin-optimization.html
If you want to reprint it, please do under the CC BY-NC-SA 4.0

Comments