Skip to content

Instantly share code, notes, and snippets.

@rniswonger rniswonger/functions.php
Last active Jul 27, 2019

Embed
What would you like to do?
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
You can’t perform that action at this time.