WordPressの管理画面ダッシュボードに「カスタム投稿タイプ毎の投稿件数」を表示する方法

WordPressの「投稿」や「固定ページ」から記事を投稿していと、管理画面のダッシュボードに投稿数がリンク付きで表示されます。

しかし、カスタム投稿タイプから記事を投稿しても、デフォルトのままではダッシュボードに投稿記事数が表示されませんので、functions.php をカスタマイズして投稿記事数をリンク付きで表示してあげると便利です。

functions.php へコード挿入

以下のコードを functions.php へ記述します。

カスタム投稿タイプが増えれば「カスタム投稿タイプ名」をカンマでつなげていきます。
[php] <?php
function custom_post_dashboard() {
$dashboard_custom_post_types= Array(
‘カスタム投稿タイプ名’
);
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’);
?>
[/php]
ダッシュボードには以下のように表示されます。
WordPressの管理画面ダッシュボードに「カスタム投稿タイプ毎の投稿件数」を表示する方法

手動でカスタム投稿タイプの設定をしている場合は、投稿タイプを増やすのに合わせてダッシュボードへの記述も足してあげれば良いですが、Types などのプラグインを利用している場合は functions.php への記述の追加も忘れずに。


コメントを残す

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください