<景品表示法に基づく表記> 本サイトのコンテンツには、商品プロモーションが含まれている場合があります。

カスタマイズ

【JIN】タイムラインを追加CSSとfunctions.phpで簡単に設定する方法

admin

時系列ごとにいい感じに並べられる表現『タイムライン』を htmlコードでいつでも表現できるよう、CSS や php のコードで仕込んでおく方法です。

なお、簡単に!と記載はありますが、下準備は必要です。まだの方は準備を済ませてから実装しましょ。表現の幅がめちゃくちゃ広がりますよ!

スポンサーリンク

事前準備

主に2つ。こちらです。

  • JIN子テーマを利用する
  • function.php のコピーを作成する(バックアップを取得する)

子テーマを利用する

「子テーマ」とは、基本となるテーマ(私の場合だと JIN)を直接編集しているコードを追記修正している場合、テーマのアップデートによってそのコードが元に戻ってしまうことを防ぎたいときに使うテーマです。子テーマに追加CSSやphp のコードをカスタマイズしておけば、親テーマがアップデートしても、子テーマが情報を引き継いでくれるようになります。

JINの子テーマをダウンロードする場合は、下記から予めダウンロードし、有効化しておきましょう。
https://jin-theme.com/jin-child-download-pages/

functions.php のバックアップを取得する

function.php はWordPress の画面からではなく、レンタルサーバ上で閲覧可能なファイルです。なので、レンタルサーバ上のファイルマネージャ等を経由してファイルを直接操作します。該当ファイルを複製し、「functions.php.bak20201226」のようにコピーの日付と合わせて名前を付けておくと後で管理は楽。

mixhost の場合、functions.php ファイルの存在場所は下記です。

/home/~<中略>~/public_html/<ドメイン名>/wp-content/themes/jin-child

タイムラインの設定手順

追加CSSとfunctions.php にソースコードを追加します。今回のこの記事では、ぽんひろさんのブログからソースコードを参考にして掲載をしています。

※Cocoon や SANGOでも稼働確認済のようです。気になる方はぽんひろさんのブログも併せてご参考ください。

追加CSSにソースコードを登録する

ダッシュボード > 外観 > カスタマイズ > 追加CSS

/*********************************
* タイムライン
*********************************/
.ptimeline-wrap{
    margin:0 auto 2rem;
}
.ptimeline-wrap .ptimeline{
    padding:0 !important;
    list-style:none !important;
}
.ptimeline-wrap .ptimeline-label {
    padding: 3px 0 0 2px;
    color: #aaa;
    font-size: 12px;
    font-weight: 500;
}
.ptimeline-wrap .ptimeline-title {
    font-size: 1.1em;
    font-weight: bold;
    line-height: 1.5;
    color:#00bfff;
}
.ptimeline-wrap .ptimeline-main {
    margin-top: 0.5em;
    padding: 0 0 1.5em;
    font-size: 0.9em;
    line-height: 1.8;
    border-bottom: dashed 1px #ddd;
    color:#555;
}
.ptimeline-wrap .ptimeline-main img{
  display:block;
  margin:1em auto;
}
.ptimeline-wrap .ptimeline-item {
    position: relative;
    padding: 0 0 1em 1.5em !important;
    margin-bottom:0 !important;
    border:none;
}
.ptimeline-wrap .ptimeline .ptimeline-item:before {
    content: "";
    width: 3px;
    background: #eee !important;
    display: block;
    position: absolute;
    top: 25px;
    bottom: -3px;
    left: 5px;
}
.ptimeline-wrap .ptimeline-item:last-child:before{
    content:none;
}
/*********************************
* タイムライン マーカー
*********************************/
.ptimeline-wrap .ptimeline-marker{
    display: block;
    position: absolute;
    content: "";
    top: 6px;
    left: 0;
    width: 14px;
    height: 14px;
    border-radius: 50%;
    border: solid 3px #00bfff;
}
.ptimeline-wrap .ptimeline-item:first-child .ptimeline-marker,.ptimeline-wrap .ptimeline-item:last-child .ptimeline-marker{
    background:#00bfff;
}
/*四角 */
.ptimeline-wrap .square .ptimeline-marker{
    border-radius: 0;
}
/* アイコン*/
.ptimeline-wrap .icon .ptimeline-item .ptimeline-marker{
    content:unset;
    border:none !important;
    background:none !important;
}
.ptimeline-wrap .icon .ptimeline-item .ptimeline-marker:before{
    font-family: "Font Awesome 5 Free";
    top: -1px;
    left: 0;
    position:absolute;
    font-weight:bold;
    font-size:16px;
    line-height:1;
    color:#00bfff;
}
.ptimeline-wrap .icon .ptimeline-item:first-child .ptimeline-marker,.ptimeline-wrap .icon .ptimeline-item:last-child .ptimeline-marker{
    background:none !important;
}
/* 画像*/
.ptimeline-wrap .tl-img .ptimeline-item .ptimeline-marker{
    content:unset;
    border:none !important;
    background:none !important;
}
.ptimeline-wrap .tl-img .ptimeline-item .ptimeline-marker:before {
    content:"";
    display:inline-block;
    background-image:url(ここに画像URL); /* 画像1番目*/
    background-size:contain;
    background-repeat:no-repeat;
    width:40px;
    height:40px;
    position: relative;
    top: -4px;
    left: -2px;
}
.ptimeline-wrap .tl-img li:nth-of-type(2) > .ptimeline-marker:before{
    background-image:url(ここに画像URL) !important; /* 画像2番目*/
}
.ptimeline-wrap .tl-img li:nth-of-type(3) > .ptimeline-marker:before{
    background-image:url(ここに画像URL) !important; /* 画像3番目*/
}
.ptimeline-wrap .tl-img li:nth-of-type(4) > .ptimeline-marker:before{
    background-image:url(ここに画像URL) !important; /* 画像4番目*/
}
.ptimeline-wrap .tl-img li:nth-of-type(5) > .ptimeline-marker:before{
    background-image:url(ここに画像URL) !important; /* 画像5番目*/
}
.ptimeline-wrap .tl-img .ptimeline-item {
    padding: 0 0 1em 3em !important;
}
.ptimeline-wrap .tl-img .ptimeline-item:before {
    top: 30px;
    left: 15px;
}
/*********************************
* タイムライン カラー
*********************************/
/* ピンク */
.ptimeline-wrap .pink .ptimeline-title{
    color:#f7bcbc !important; /* タイトル色 */
}
.ptimeline-wrap .pink .ptimeline-main{
    color:#555 !important; /* コンテンツ色 */
}
.ptimeline-wrap .pink .ptimeline-marker{
    border: solid 3px #f7bcbc !important; /* マーカー色 */
    color:#f7bcbc; /* アイコン色 */
}
.ptimeline-wrap .pink .ptimeline-item:first-child .ptimeline-marker,.ptimeline-wrap .pink .ptimeline-item:last-child .ptimeline-marker{
    background:#f7bcbc; /* マーカー色(最初と最後)*/
}
.ptimeline-wrap .pink .ptimeline-item .ptimeline-marker:before{
    color:#f7bcbc !important; /* アイコン色 */
}
/*イエロー*/
.ptimeline-wrap .yellow .ptimeline-title{
    color:#fdc44f !important; /* タイトル色 */
}
.ptimeline-wrap .yellow .ptimeline-main{
    color:#555 !important; /* コンテンツ色 */
}
.ptimeline-wrap .yellow .ptimeline-marker{
    border: solid 3px #fdc44f !important; /* マーカー色 */
    color:#fdc44f; /* アイコン色 */
}
.ptimeline-wrap .yellow .ptimeline-item:first-child .ptimeline-marker,.ptimeline-wrap .yellow .ptimeline-item:last-child .ptimeline-marker{
    background:#fdc44f; /* マーカー色(最初と最後)*/
}
.ptimeline-wrap .yellow .ptimeline-item .ptimeline-marker:before{
    color:#fdc44f !important; /* アイコン色 */
}
/*グリーン*/
.ptimeline-wrap .green .ptimeline-title{
    color:#2fcdb4 !important; /* タイトル色 */
}
.ptimeline-wrap .green .ptimeline-main{
    color:#555 !important; /* コンテンツ色 */
}
.ptimeline-wrap .green .ptimeline-marker{
    border: solid 3px #2fcdb4 !important; /* マーカー色 */
    color:#2fcdb4; /* アイコン色 */
}
.ptimeline-wrap .green .ptimeline-item:first-child .ptimeline-marker,.ptimeline-wrap .green .ptimeline-item:last-child .ptimeline-marker{
    background:#2fcdb4; /* マーカー色(最初と最後)*/
}
.ptimeline-wrap .green .ptimeline-item .ptimeline-marker:before{
    color:#2fcdb4 !important; /* アイコン色 */
}

ここまでコピペができたら、「公開」のボタンを押してOK。

functions.phpにソースコードを登録する

先ほどバックアップをとりましたので、「functions.php」に直接下記のコードを追記し、保存します。

//timelineショートコードコンテンツ内に余計な改行や文字列が入らないように除外
if ( !function_exists( 'remove_wrap_shortcode_wpautop' ) ):
function remove_wrap_shortcode_wpautop($shortcode, $content){
  //tiショートコードのみを抽出
  $pattern = '/\['.$shortcode.'.*?\].*?\[\/'.$shortcode.'\]/is';
  if (preg_match_all($pattern, $content, $m)) {
    $all = null;
    foreach ($m[0] as $code) {
      $all .= $code;
    }
    return $all;
  }
}
endif;
//タイムラインショートコード
add_shortcode('ptimeline', 'tl_shortcode');
if ( !function_exists( 'tl_shortcode' ) ):
function tl_shortcode( $atts, $content = null ){
extract( shortcode_atts( array(
    'color' => null,
    'marker' => null,
    'icon' => null,
  ), $atts ) );
  $content = remove_wrap_shortcode_wpautop('ti', $content);
  $content = do_shortcode( shortcode_unautop( $content ) );
  if ( $icon ):
   $tag .= '<style type="text/css">.ptimeline-wrap .'.$icon.' .ptimeline-item .ptimeline-marker:before{ content:"\\'.$icon.'";}</style>';
  endif;
  $tag .= '<div class="ptimeline-wrap">'.
            '<ul class="ptimeline '.$color.' '.$marker.' '.$icon.'">'.
              $content.
            '</ul>'.
          '</div>';
  return apply_filters('timeline_tag', $tag);
}
endif;
 
//タイムラインショートコード中身
add_shortcode('ti', 'tl_item_shortcode');
if ( !function_exists( 'tl_item_shortcode' ) ):
function tl_item_shortcode( $atts, $content = null ){
  extract( shortcode_atts( array(
    'title' => null,
    'label' => null,
    'icon_item' => null,
  ), $atts ) );
  $content = do_shortcode( shortcode_unautop( $content ) );
  if ($icon_item):
   $tag .= '<style type="text/css">.ptimeline-wrap .ptimeline-item .'.$icon_item.':before{ content:"\\'.$icon_item.'" !important;}</style>';
  endif;
  $tag .= '<li class="ptimeline-item">'.
            '<div class="ptimeline-label">'.$label.'</div>'.
             '<div class="ptimeline-title">'.$title.'</div>'.
             '<div class="ptimeline-main">'.$content.'</div>'.
            '<div class="ptimeline-marker '.$icon_item.'"></div>'.
          '</li>';
  return apply_filters('timeline_item_tag', $tag);
}
endif;

稼働確認

html のソースコードを入力します。
(ビジュアルエディタの場合、カスタムhtml で入力します)

ソースコード例

[ptimeline]
[ti label="STEP1" title="タイトル1"]ここにテキストここにテキストここにテキスト[/ti]
[ti label="STEP2" title="タイトル2"]ここにテキストここにテキストここにテキスト[/ti]
[/ptimeline]

結果

※JIN:Rへ移行したため、結果例は画像イメージとなりますがご了承ください。

完成!

設定はここまでで一通りは完了です。ぽんひろさんは、他にもいろいろなカスタマイズができるようにソースコードを作ってくださっています。

今回はメインの表現ができるところまで、ですが、他の表現も見てみたい方は是非ぽんひろさんのブログサイトもご参考ください。

追記(JIN:Rの場合)

JIN:Rの場合、この設定をしなくてもタイムライン表示用の機能が装備されています。

スポンサーリンク
ABOUT ME
加藤ミサ
加藤ミサ
katomisa
趣味がなくてブログを始めたワーママブロガー。当サイト「朝ライフ(AsaLife)」を運営するうちに、ストックイラストに出会いました。「ブログ制作をきっかけに趣味を探したい人」もしくは「趣味をきっかけにブログでも情報を発信したい人」に向けて、ブログ制作にまつわる情報を発信しています。美容家SEとしても活動中。

・ブログガイド
 https://asalife.jp/blog-guide-top
・かとみさイラスト制作室
 https://katomisaillust.asalife.jp
・note
 https://note.com/katomisa3
記事URLをコピーしました