WordPressテーマでタイムライン表現を使うためのショートコード実装方法

タイムライン

先日、自作テーマのフォーラムに「タイムライン機能があったらいいな」というリクエストをいただきました。

で、ショートコードを用いて実装してみたら、思っていたよりは簡単に実装できました。

加えて、コピペでどんなテーマにも実装できそうだったので、カスタマイズ方法の紹介です。

こんな感じのタイムラインを作成できます。

[timeline title=”テストタイムライン”]

  • 手順1
    プラグインをインストールする
    事前準備としてまずプラグインをインストールする必要があります。

  • 手順2
    プラグインでバックアップ
    バックアッププラグインをインストールしたら、ボタン押してバックアップをしましょう。

  • [/timeline]

    スポンサーリンク
    レクタングル(大)広告

    主な手順

    カスタマイズに必要となる主な手順はこちら。

    1. ショートコード用のコードをfunctions.phpにコピペ
    2. タイムライン用のCSSをstyle.cssにコピペ

    特に書かれている内容を気にしないのであれば、コピペ2回程度でカスタマイズできるようになっています。

    ショートコード用のコードを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;
    
    //タイムラインの作成(timelineショートコード)
    add_shortcode('timeline', 'timeline_shortcode');
    if ( !function_exists( 'timeline_shortcode' ) ):
    function timeline_shortcode( $atts, $content = null ){
      extract( shortcode_atts( array(
        'title' => null,
      ), $atts ) );
      $content = remove_wrap_shortcode_wpautop('ti', $content);
      $content = do_shortcode( shortcode_unautop( $content ) );
      $title_tag = null;
      if ($title) {
        $title_tag = '<div class="timeline-title">'.$title.'</div>';
      }
      $tag = '<div class="timeline-box">'.
                $title_tag.
                '<ul class="timeline">'.
                  $content.
                '</ul>'.
              '</div>';
      return apply_filters('timeline_tag', $tag);
    }
    endif;
    
    //タイムラインアイテム作成(タイムラインの中の項目)
    add_shortcode('ti', 'timeline_item_shortcode');
    if ( !function_exists( 'timeline_item_shortcode' ) ):
    function timeline_item_shortcode( $atts, $content = null ){
      extract( shortcode_atts( array(
        'title' => null,
        'label' => null,
      ), $atts ) );
      $title_tag = null;
      if ($title) {
        $title_tag = '<div class="timeline-item-title">'.$title.'</div>';
      }
    
      $content = do_shortcode( shortcode_unautop( $content ) );
      $tag = '<li class="timeline-item">'.
                '<div class="timeline-item-label">'.$label.'</div>'.
                '<div class="timeline-item-content">'.
                  '<div class="timeline-item-title">'.$title.'</div>'.
                  '<div class="timeline-item-snippet">'.$content.'</div>'.
                '</div>'.
              '</li>';
      return apply_filters('timeline_item_tag', $tag);
    }
    endif;
    
    サンプルなので、ショートコードオプション用のサニタイズ処理などは行っていません。

    タイムライン用のCSSをstyle.cssにコピペ

    次は、タイムラインショートコードが出力するHTMLの装飾です。

    以下のCSSをテーマ(子テーマ)のstyle.cssにコピペしてください。

    /*********************************
    * タイムライン
    *********************************/
    .timeline-box {
      margin-bottom: 20px;
      border: 1px solid #ccc;
      border-radius: 4px;
      padding: 16px 5px;
      box-sizing: border-box;
    }
    
    .timeline-box *{
      box-sizing: border-box;
    }
    
    .timeline-box .timeline {
      list-style: none;
      padding: 0;
      margin: 0;
    }
    
    .timeline-title {
      font-weight: bold;
      font-size: 1.1em;
      text-align: center;
    }
    
    .timeline > li {
      margin-bottom: 60px;
    }
    
    .timeline > li.timeline-item {
      overflow: hidden;
      margin: 0;
      position: relative;
    }
    
    .timeline-item-label {
      width: 110px;
      float: left;
      padding-top: 18px;
      text-align: right;
      padding-right: 1em;
      font-size: 14px;
    }
    
    .timeline-item-title {
      font-weight: bold;
    }
    
    .timeline-item-content {
      width: calc(100% - 110px);
      float: left;
      padding: .8em 1.4em;
      border-left: 3px #e5e5d1 solid;
    }
    
    .timeline-item:before {
      content: '';
      width: 12px;
      height: 12px;
      background: #6fc173;
      position: absolute;
      left: 105px;
      top: 24px;
      border-radius: 100%;
    }
    
    /* for Smartphone */
    @media screen and (max-width: 480px) {
      .timeline-box .timeline {
        padding-left: 10px;
      }
    
      .timeline > li.timeline-item {
        overflow: visible;
        border-left: 3px #e5e5d1 solid;
      }
    
      .timeline-item-label {
        width: auto;
        float: none;
        text-align: left;
        padding-left: 16px;
      }
    
      .timeline-item-content {
        width: auto;
        padding: 8px;
        float: none;
        border: none;
      }
    
      .timeline-item::before {
        left: -12px;
        top: 19px;
        width: 21px;
        height: 21px;
      }
    }

    テーマのスタイルによっては、装飾がずれる可能性があるので、テーマごとに調整してください。上記コードは、WordPress公式テーマTwenty
    Seventeenで動作確認しています。

    全てのテーマで動作する保証はできません。

    動作確認

    タイムラインショートコードを動作確認するとこんな感じで表示されます。

    [timeline title=”これまでの経歴(※フィクションです)”]

  • 2016年3月
    不祥事により会社を退職
    取引先の社長の頭をひっぱたいてしまい会社をクビに。

  • 2016年4月
    就職活動
    就職活動を行うも、前の会社での行いが知れ渡っており、同業者は全て門前払い。

  • 2016年8月
    就職を諦め無職へ
    完全にやる気が無くなり無職へジョブチェンジ。毎日オンラインゲームざんまいの濃密な日々を過ごす。

  • 2017年8月
    サイト運営を開始
    貯金を使い果たしそうになり「何とかせねば!」と〇〇というサイトを開始。

  • 2018年8月
    サイト運営1年経過
    毎日記事を書き続けた結果、月収〇〇円に。節約すれば何とか生活できる収入に到達。

  • [/timeline]

    モバイルではこんな感じで表示されます。

    タイムラインのモバイル表示

    ショートコード例はこちら。

    [[timeline title="これまでの経歴(※フィクションです)"]
       
  • 2016年3月
    不祥事により会社を退職
    取引先の社長の頭をひっぱたいてしまい責任を取って退職。
  • 2016年4月
    就職活動
    就職活動を行うも、前の会社での行いが知れ渡っており、同業者は全て門前払い。
  • 2016年8月
    就職を諦め無職へ
    完全にやる気が無くなり無職へジョブチェンジ。毎日オンラインゲームざんまいの濃密な日々を過ごす。
  • 2017年8月
    サイト運営を開始
    貯金を使い果たしそうになり「何とかせねば!」と〇〇というサイトを開始。
  • 2018年8月
    サイト運営1年経過
    毎日記事を書き続けた結果、月収〇〇円に。節約すれば何とか生活できる収入に到達。
  • [/timeline]]

    参考

    このタイムラインのデザインはWebクリエイターボックスを運営されているManaさんが作成されたCodePenのものを参考に、モバイル用のスタイルを加えさせていただきました。

    まとめ

    今回のタイムラインショートコードは、「順番を扱う内容」、「時系列を扱う内容」のコンテンツを作成する際に、内容分かりやすく装飾してくれるかと思います。

    そんなわけで、「手順や時系列を出来る限りわかりやすく表現したい」なんて場合に良い表現方法になるかと思います。

    スポンサーリンク
    レクタングル(大)広告
    レクタングル(大)広告

    フォローする

    スポンサーリンク

    『WordPressテーマでタイムライン表現を使うためのショートコード実装方法』へのコメント

    1. 名前:tk 投稿日:2022/11/17(木) 00:46:14 ID:f36b492d7

      ショートコードの作成方法が載っていないのですが、どのようにショートコードを本文内に設置すれば良いのでしょうか?

    2. アバター画像 名前:わいひら 投稿日:2022/11/19(土) 10:42:59 ID:e7d1d2352

      ご連絡ありがとうございます。
      ショートコードが実行され出力されてしまっていたようです。
      記事の内容の「ショートコード例」を修正しておきました。
      https://nelog.jp/timeline-shortcode#%E5%8B%95%E4%BD%9C%E7%A2%BA%E8%AA%8D