最終更新日をソート並び替え機能付で表示させたいとき
記事メンテナンスを行う時に、この記事は最後いつ更新したのかなというのがひと目でわかるようにカスタマイズする方法です。記事が何百記事にもなるとどこをどう修正してよいかわからなくなるので時系列で並び替えることでタスクを洗い出せます。どの記事から修正すればいいかの優先順位付けができて作業がスムーズにすすみます。
function.phpに以下のコードを入力すると最終更新日が表示され、並び替えソート機能が使えるようになります。
コピペで使えますので子テーマなどでぜひご活用ください。
// 最終更新日と並び替え機能を管理画面に表示する -------------------------------------------------------add_filter( 'manage_edit-post_columns', 'aco_last_modified_admin_column' ); // Create the last modified columnfunction aco_last_modified_admin_column( $columns ) { $columns['modified-last'] =__( '最終更新日', 'aco' ); return $columns;} add_filter( 'manage_edit-post_sortable_columns', 'aco_sortable_last_modified_column' ); // Allow that content to be sortable by modified time informationfunction aco_sortable_last_modified_column( $columns ) { $columns['modified-last'] = 'modified'; return $columns;} add_action( 'manage_posts_custom_column', 'aco_last_modified_admin_column_content', 10, 2 ); // Format the outputfunction aco_last_modified_admin_column_content( $column_name, $post_id ) {// Do not continue if this is not the modified column if ( 'modified-last' != $column_name ) return;$modified_date= the_modified_date( 'Y年Md日Ag時i分' );echo $modified_date; }
コードを記述すると、投稿一覧画面で以下のように表示されます。最終更新日横の三角ボタンを押すと時系列で並び替えができます。
右上の表示オプションで表示項目を取捨選択できます。

難しい方は、並び替え機能はないですがAdmin Columnsのプラグインを使えば日時は表示されます。
文字数を記事投稿一覧に表示させたいとき
ワードプレスのプラグインであるPosts Character Count Adminを使ってもできますが、ここではfunction.phpに直接コード記述する方法もご紹介します。
// 投稿一覧に文字数追加 -------------------------------------------------------function add_posts_columns_count($columns) { $columns['count'] = '文字数'; return $columns;}function add_posts_columns_count_row($column_name, $post_id) { if( 'count' == $column_name ) { $count = mb_strlen(strip_tags(get_post_field('post_content', $post_id))); echo $count; }}add_filter( 'manage_posts_columns', 'add_posts_columns_count' );add_action( 'manage_posts_custom_column', 'add_posts_columns_count_row', 10, 2 );
タイトル文字カウンターを記事投稿画面に表示させたいとき
スマホでみられることを想定して、タイトル文字数を32文字目安にしている方もおおくいらっしゃるのではないでしょうか。
記事投稿画面でタイトル文字数を一目でチェックできるカスタマイズは以下コードをfunction.phpに記述するとよいです。全角半角共に1文字とカウントされます。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Wordpress投稿画面のタイトル文字数をカウントする | |
function count_title_characters() {?> | |
<script type="text/javascript"> | |
jQuery(document).ready(function($){ | |
//in_selの文字数をカウントしてout_selに出力する | |
function count_characters(in_sel, out_sel) { | |
$(out_sel).html( $(in_sel).val().length ); | |
} | |
//ページ表示に表示エリアを出力 | |
$('#titlewrap').after('<div style="position:absolute;top:-24px;right:0;color:#666;background-color:#f7f7f7;padding:1px 2px;border-radius:5px;border:1px solid #ccc;">文字数<span class="wp-title-count" style="margin-left:5px;">0</span></div>'); | |
//ページ表示時に数える | |
count_characters('#title', '.wp-title-count'); | |
//入力フォーム変更時に数える | |
$('#title').bind("keydown keyup keypress change",function(){ | |
count_characters('#title', '.wp-title-count'); | |
}); | |
}); | |
</script><?php | |
} | |
add_action( 'admin_head-post-new.php', 'count_title_characters' ); | |
add_action( 'admin_head-post.php', 'count_title_characters' ); |