• RSS

[WP]カスタム投稿タイプの投稿数をダッシュボードの「概要」に表示する方法

  • このエントリーをはてなブックマークに追加
  • follow us in feedly

WordPress のバージョン3.8 で、管理画面のダッシュボードの「概要」にカスタム投稿タイプの記事数を表示させる方法のご紹介です。

目次

functions.php の編集

ご利用のテーマのfuntions.php を編集します。
add_filter( 'dashboard_glance_items', 'mytheme_dashboard_glance_items' );
function mytheme_dashboard_glance_items( $elements ) {
	foreach ( array(
    'カスタム投稿タイプ1',
    'カスタム投稿タイプ2'
	) as $post_type ) {
		$num_posts = wp_count_posts( $post_type );
		if ( $num_posts && $num_posts->publish ) {
			$post_type_object = get_post_type_object($post_type);
			$post_type_label = $post_type_object->label;
			$text = number_format_i18n( $num_posts->publish ).'件の '.$post_type_label.'の投稿';
			$elements[] = sprintf( '<a href="edit.php?post_type=%1$s" class="%1$s-count">%2$s</a>', $post_type, $text );
		}
	}
	return $elements;
}
カスタム投稿タイプ1,カスタム投稿タイプ2…のように、カンマ区切りで複数の投稿タイプを指定することが可能です。

WordPress 4.2 での注意事項

WordPress のバージョン4.2 以降はこのカスタマイズが使えなくなりました。

当方はTypes プラグインでカスタム投稿タイプを管理しているのですが、
Types の最新バージョン(1.6.6.2)以降であれば、ダッシュボードの「概要」にカスタム投稿タイプの記事数を表示させることができるようになります。

詳しくはこちらの記事を参考にしてみてください。



  • このエントリーをはてなブックマークに追加
  • follow us in feedly

コメント

コメントを残す