WP widget アーカイブ(年) 20230222実装コード

Quick Edit Pencil


アーカイブ(年)
<?php

// ウィジェットと同義の設定
$instance= array(
  //※'title'       => '',   // タイトル ※フックで指定する
  'dropdown'    => 1,    // ドロップダウン表示 
  'count'       => 1     // 投稿数を表示
);

// 必要があれば設定する
$args = array();

// class WP_Widget_Archives
add_filter( 'widget_title', 'hook_widget_title' );// タイトルをフック
add_filter( 'widget_archives_dropdown_args', 'hook_widget_archives_args' );// drop 月から年へ
add_filter( 'widget_archives_args', 'hook_widget_archives_args' );// flat 月から年へ
// wp_get_archives
//add_filter( 'getarchives_where', 'cat_limited_wp_get_archives' );

// クラスを実体化する
$my_archives1 = new WP_Widget_Archives();
// メソッド(関数)を起動する
$my_archives1->widget( $args, $instance );

// class WP_Widget_Archives
remove_filter( 'widget_title', 'hook_widget_title' );
remove_filter( 'widget_archives_dropdown_args', 'hook_widget_archives_args' );
remove_filter( 'widget_archives_args', 'hook_widget_archives_args' );
// wp_get_archives
//remove_filter( 'getarchives_where', 'cat_limited_wp_get_archives' );

// フック・カスタム関数
function hook_widget_title( $title ) {
    // タイトルを変更する
    $title = '';
    return $title;
}
function hook_widget_archives_args( $args ) {
    // デフォルトの月別(type=monthly)から年別のアーカイブに変更
    $args['type'] = 'yearly';
    return $args;
}

?>

<script type='text/javascript'>
/* <![CDATA[ */
(function() {
    {
        // この関数の呼び出し位置を取得し
        var scpt,s;
        scpt = document.getElementsByTagName("script");
        scpt = scpt[scpt.length-2];
        s = scpt;
    }
    // scriptタグに隣接する前の要素を取得
	var dropdown = s.previousElementSibling;
    for( var i = 0; i < dropdown.length; i++ ){
        var t = dropdown.options[i].text;
        var matches = t.match(/\d{4}/);
        for(r in matches){
            if(r==0){
                var y = matches[r];
                //console.log(y);
                y = parseInt(y);
                if (isNaN(y)) { s = "数字を入力して下さい。"; }
                else if (y > 1988) { s = "平成"+(y-1988)+"年"; }
                else if (y > 1925) { s = "昭和"+(y-1925)+"年"; }
                else if (y > 1911) { s = "大正"+(y-1911)+"年"; }
                else if (y > 1867) { s = "明治"+(y-1867)+"年"; }
                else if (y > 1864) { s = "慶応"+(y-1864)+"年"; }
                else if (y > 1863) { s = "元治"+(y-1863)+"年"; }
                else if (y > 1860) { s = "文久"+(y-1860)+"年"; }
                else if (y > 1853) { s = "安政"+(y-1853)+"年"; }
                else if (y > 1847) { s = "嘉永"+(y-1847)+"年"; }
                else { s = "1848(嘉永元年)より大きい年数を指定下さい。"; }
                //console.log(s);
                dropdown.options[i].text = t.replace(/(\d{4})/, y+'年 / '+s);
                break;
            }
        };
    }
})();
/* ]]> */
</script>

このブログの人気の投稿

PowerShellのGetDetailsOf メソッドでプロパティの詳細情報のID番号と項目名を列挙します

書字方向 横書方向変換スクリプト 左書きから右書きへ(コピペ用途)

PowerShellで複数ファイルのプロパティを取得する方法(準備編)

簡単 YouTube動画をダウンロード、音声のみ保存する方法 2019

DOMノードオブジェクトを文字列に変換する

AppleScript 改行 コード 置換

Blender: 辺の長さを数値で指定するアドオン

Powershell: プロパティの詳細情報インデックスと項目名の列挙

VMWare Playerでホストとゲスト間でクリップボードが共有できない時の対処法