Return to Snippet

Revision: 62379
at February 20, 2013 07:05 by bitsculptor


Initial Code
add_filter( 'manage_edit-events_columns', 'my_edit_events_columns' ) ;

function my_edit_events_columns( $columns ) {

	$columns = array(
		'cb' => '<input type="checkbox" />',
		'title' => __( 'Event' ),
		'event-date' => __( 'Event Date' ),
		'category' => __( 'Category' ),
		'date' => __( 'Date' )
	);

	return $columns;
}

add_action( 'manage_events_posts_custom_column', 'my_manage_events_columns', 10, 2 );

function my_manage_events_columns( $column, $post_id ) {
	global $post;

	switch( $column ) {

		/* If displaying the 'duration' column. */
		case 'event-date' :

			/* Get the post meta. */
			$event_date = get_post_meta( $post_id, '_event_date', true );

			/* If no duration is found, output a default message. */
			if ( empty( $event_date ) )
				echo __( 'Unknown' );

			/* If there is a duration, append 'minutes' to the text string. */
			else
				echo( $event_date );

			break;

		/* If displaying the 'genre' column. */
		case 'category' :

			/* Get the genres for the post. */
			$terms = get_the_terms( $post_id, 'event-category' );

			/* If terms were found. */
			if ( !empty( $terms ) ) {

				$out = array();

				/* Loop through each term, linking to the 'edit posts' page for the specific term. */
				foreach ( $terms as $term ) {
					$out[] = sprintf( '<a href="%s">%s</a>',
						esc_url( add_query_arg( array( 'post_type' => $post->post_type, 'event-category' => $term->slug ), 'edit.php' ) ),
						esc_html( sanitize_term_field( 'name', $term->name, $term->term_id, 'event-category', 'display' ) )
					);
				}

				/* Join the terms, separating them with a comma. */
				echo join( ', ', $out );
			}

			/* If no terms were found, output a default message. */
			else {
				_e( 'No Categories' );
			}

			break;

		/* Just break out of the switch statement for everything else. */
		default :
			break;
	}
}

Initial URL

                                

Initial Description
This adds custom columns to a custom post type

Initial Title
Custom columns for wordpress custom post type

Initial Tags
post, wordpress

Initial Language
PHP