Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rniswonger/072f1ec8e165cec7563357a9ed6700ba to your computer and use it in GitHub Desktop.
Save rniswonger/072f1ec8e165cec7563357a9ed6700ba to your computer and use it in GitHub Desktop.
WordPress: Add admin columns for a custom post type
<?php
/**
* CPT: Custom columns for post_slug
* replace all occurances of "post_slug" with your post type's slug and "theme_domain" with your theme's domain
*/
function set_custom_edit_post_slug_columns( $columns ) {
// unset( $columns['date'] ); // disable existing column
$columns['image'] = __( 'Image', 'theme_domain' );
$columns['date'] = __( 'Custom Date', 'theme_domain' );
return $columns;
}
add_filter( 'manage_post_slug_posts_columns', __NAMESPACE__ . '\\set_custom_edit_post_slug_columns' );
function custom_post_slug_column( $column, $post_id ) {
switch( $column ) {
case 'image':
$image_url = get_field( 'slide_image_1' );
if ( $image_url ) {
echo '<img src="' . $image_url . '" style="width:200px; height:auto;">';
}
break;
case 'date':
$datetime = get_field( 'slide_starts', $post_id );
if ( $datetime ) {
echo date( 'm/d/Y g:i:s a', $datetime );
} else {
echo '-';
}
break;
}
}
add_action( 'manage_post_slug_posts_custom_column', __NAMESPACE__.'\\custom_post_slug_column', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment