CCDzine
5/7/2015 - 7:59 AM

Display All Custom Post Types in ‘At A Glance’ Dashboard Widget. Another version of this snippet has a translation function with variables,

Display All Custom Post Types in ‘At A Glance’ Dashboard Widget. Another version of this snippet has a translation function with variables, so it doesn't abide by WordPress coding standards. This replaces that function with a conditional statement. Compare in revisions.

<?php

function ccd_cpt_to_at_glance() {
	$args = array(
		'public' => true,
		'_builtin' => false,
		'show_ui'  => true
	);
	$output = 'object';
	$operator = 'and';
	$post_types = get_post_types( $args, $output, $operator );
	
	foreach ( $post_types as $post_type ) {
	
		$num_posts = wp_count_posts( $post_type->name );
		$num = number_format_i18n( $num_posts->publish );
		$count = intval( $num_posts->publish );

		if ( $num < 1 ) {
		  $post_type_name = $post_type->labels->name;
		} elseif ( $num > 1 ) {
		  $post_type_name = $post_type->labels->name;
		} else {
		  $post_type_name = $post_type->labels->singular_name;
		}
		
		if ( current_user_can( 'edit_posts' ) ) {
			$output = '<a href="edit.php?post_type=' . $post_type->name . '">' . $count . ' ' . $post_type_name . '</a>';
			echo '<li class="post-count ' . $post_type->name . '-count">' . $output . '</li>';
		} else {
			$output = '<span>' . $count . ' ' . $post_type_name . '</span>';
			echo '<li class="post-count ' . $post_type->name . '-count">' . $output . '</li>';
		}
	}
}
add_action( 'dashboard_glance_items', 'ccd_cpt_to_at_glance' );

?>

Display All Custom Post Types in ‘At a Glance’ Dashboard Widget

WordPress Snippet