そういうのがいいブログ

SIerで働く30代サラリーマンSEがインフラエンジニアに憧れてLinux・クラウド・AWSの勉強をするブログ

【はてなブログ】更新日を表示するカスタマイズ法(Minimalism対応)

※[PR]当ブログの記事の中にはプロモーションが含まれています。

f:id:souiunogaii:20191214212728j:plain

 

はてなブログで、更新日を表示させるカスタマイズ方法(テーマMinimalismに対応)

SEO対策のために、ブログの投稿日の隣に、更新日を表示させるカスタマイズ方法の紹介です。

参考にしたサイト

www.tsubasa-note.blog

www.tomomore.com

note.com

具体的な手順

  • ①: <head>部への追加
  • ②: 記事下部への追加
  • ③: CSSへの追加

手順①: <head>部への追加

設定 > 詳細設定 > 検索エンジン最適化 > headに要素を追加

の部分に下記を追記します。

<!--jQuery 読み込み-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!--Font Awesome 読み込み-->
<link href="https://use.fontawesome.com/releases/v5.8.1/css/all.css" rel="stylesheet">

f:id:souiunogaii:20191214204211j:plain

f:id:souiunogaii:20191214212613j:plain
f:id:souiunogaii:20191214204554j:plain

手順②: 記事下部への追加

デザイン > カスタマイズ > 記事 > 記事下

の部分に下記を追記します。

※「url = 'https://~~~/sitemap.xml'」の部分だけは、自信のブログのURLに修正が必要です。※

<!-- ↓----記事の更新日時表示----↓ -->
<script>
/*
 * Show lastmod for Hatena Blog v1.0.1
 * Date: 2016-12-20
 * Copyright (c) 2016 https://www.tsubasa-note.blog/
 * Released under the MIT license:
 * http://opensource.org/licenses/mit-license.php
 */
;(function($) {
    'use strict';
    var urls = [], opts = {cache: false, dataType: 'xml'}, p,
    url = 'https://XXXXXXXXXXXXXXXXXXXXXX.hatenablog.com/sitemap.xml'; //自分のブログURL+「/sitemap.xml」
    function parseSitemapXML(url) {
        var d = new $.Deferred;
        $.ajax($.extend(opts, {
            url: url
        })).done(function(xml) {
            $(xml).find('sitemap').each(function() {
                urls.push($(this).find('loc').text());
            });
            d.resolve();
        }).fail(function() {
            d.reject();
        });
        return d.promise();
    }
    function findURL(url) {
        $.ajax($.extend(opts, {
            url: url
        })).done(function(xml) {
            var isMatched = false;
            $(xml).find('url').each(function() {
                var $this = $(this);
                if ($this.find('loc').text() === location.href) {
                    isMatched = true;
                    appendLastmod($this.find('lastmod').text());
                    return false;
                }
            });
            if (!isMatched) nextURL();
        }).fail(function() {});
    }
    function nextURL() {
        urls.shift();
        if (urls.length) findURL(urls[0]);
    }
    function appendLastmod(lastmod) {
        var $container = $('<div></div>', {'class': 'lastmod'}).text(lastmod.replace(/T.*0/,""));
        if ($('.entry-header > .date').get(0).tagName.toLowerCase() === 'span') {
            $('.entry-title').before($container);
        } else {
            $('.entry-date').append($container);
        }
    }
    p = parseSitemapXML(url);
    p.done(function() {findURL(urls[0])});
    p.fail(function(error) {});
})(jQuery);
</script>
<!-- ↑----記事の更新日時表示----↑ -->

f:id:souiunogaii:20191214205701j:plain

f:id:souiunogaii:20191214205838j:plain

f:id:souiunogaii:20191214210235j:plain

f:id:souiunogaii:20191214210003j:plain

手順③: CSSへの追加

デザイン > カスタマイズ > デザインCSS の部分に下記を追記します。

/*↓----更新日時表示----↓*/
.lastmod {
background-color: transparent;
padding: 5px 0px;
text-decoration: none;
font-size: 15px;
display: inline;
margin-left: 0px;
color: #888888;
}
.lastmod::before {
margin-right: 5px;
margin-left: 10px;
padding-left: 3px;
font-family: "Font Awesome 5 Free";
font-weight: bold;
content: '\f01e';
}
.entry-date a {
background-color: transparent;
padding: 5px 0px 5px 6px;
text-decoration: none;
font-size: 15px;
display: inline;
}
.entry-date a::before {
margin-right: 5px;
padding-left: 3px;
}
/*↑----更新日時表示----↑*/

f:id:souiunogaii:20191214211244j:plain