WordPress のカスタム投稿タイプの記事数をダッシュボードに一括で表示設定する方法

カスタム投稿タイプの投稿数をダッシュボードに表示するには以下の記事で紹介されているコードをそのまま貼りつければ実現可能ですが、たくさんある場合は、何度も何度もこのコードを書かなければいけませんね。
カスタム投稿タイプの投稿数をダッシュボードに表示する

そのため、勝手ながら紹介されているコードを少し改造しました。連想配列にカスタム投稿タイプ名を入れることで、一括で表示設定ができます。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
function custom_post_dashboard() {
 
	$dashboard_custom_post_types= Array(
		'news-hoge',
		'news-fuga',
		'news-piyo',
	);
 
	foreach($dashboard_custom_post_types as $custom_post_type) {
		global $wp_post_types;
		$num_post_type = wp_count_posts($custom_post_type);
		$num = number_format_i18n($num_post_type->publish);
		$text = _n( $wp_post_types[$custom_post_type]->labels->singular_name, $wp_post_types[$custom_post_type]->labels->name, $num_post_type->publish );
		$capability = $wp_post_types[$custom_post_type]->cap->edit_posts;
 
		if (current_user_can($capability)) {
			$num = "<a href='edit.php?post_type=" . $custom_post_type . "'>$num</a>";
			$text = "<a href='edit.php?post_type=" . $custom_post_type . "'>$text</a>";
		}
 
		echo '<tr>';
		echo '<td class="first b b_' . $custom_post_type . '">' . $num . '</td>';
		echo '<td class="t ' . $custom_post_type . '">' . $text . '</td>';
		echo '</tr>';
	}
}
add_action('right_now_content_table_end', 'custom_post_dashboard');

コメント

コメントは受け付けていません。