Last active
March 30, 2022 17:49
-
-
Save rniswonger/072f1ec8e165cec7563357a9ed6700ba to your computer and use it in GitHub Desktop.
WordPress: Add admin columns for a custom post type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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