Skip to content

Instantly share code, notes, and snippets.

@anilmeena
Created October 21, 2016 03:00
Show Gist options
  • Select an option

  • Save anilmeena/b62b2ed164213a91409c4944b4e21b86 to your computer and use it in GitHub Desktop.

Select an option

Save anilmeena/b62b2ed164213a91409c4944b4e21b86 to your computer and use it in GitHub Desktop.
Add custom column in custom post type
<?php
// Add custom column in custom post type (here custom_post = latest_at_sonic)
add_filter( 'manage_edit-latest_at_sonic_columns', 'my_edit_latest_at_sonic_columns' ) ;
function my_edit_latest_at_sonic_columns( $columns ) {
$columns = array(
'cb' => '<input type="checkbox" />',
'title' => __( 'Title' ),
'description' => __( 'Description' ),
'thumbnail' => __( 'Thumbnail' ),
'date' => __( 'Date' )
);
return $columns;
}
add_action( 'manage_latest_at_sonic_posts_custom_column', 'my_manage_latest_at_sonic_columns', 10, 2 );
function my_manage_latest_at_sonic_columns( $column, $post_id ) {
global $post;
switch( $column ) {
/* If displaying the 'description' column. */
case 'description' :
/* Get the post meta. */
$description = get_the_content( $post_id );
$short_desc = substr($description, 0, 80);
/* If no short_desc is found, output a default message. */
if ( empty( $short_desc ) )
echo __( '-' );
/* If there is a short_desc, append 'minutes' to the text string. */
else
printf( __( '%s' ), $short_desc );
break;
/* If displaying the 'thumbnail' column. */
case 'thumbnail' :
/* Get the genres for the post. */
$thumbnail = get_the_post_thumbnail( $post_id, array( 100, 100) );
/* If terms were found. */
if ( !empty( $thumbnail ) ) {
/* Join the terms, separating them with a comma. */
echo $thumbnail;
}
/* If no terms were found, output a default message. */
else {
_e( '-' );
}
break;
/* Just break out of the switch statement for everything else. */
default :
break;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment