Last active
December 15, 2018 13:17
-
-
Save adamlaki/6fb5c54827699a2d0bee7171c9380e00 to your computer and use it in GitHub Desktop.
Add Custom Columns To Post Type
This file contains hidden or 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 | |
// Add the Terms(s) columns to the snippets post type | |
function set_custom_edit_snippets_columns($columns) { | |
$columns['terms'] = __( 'Term(s)', 'pine' ); | |
return $columns; | |
} | |
add_filter( 'manage_snippets_posts_columns', 'set_custom_edit_snippets_columns' ); | |
// Add the data to the snippet's Term(s) column | |
function custom_snippets_column( $column, $post_id ) { | |
switch ( $column ) { | |
case 'terms' : | |
$terms = get_the_terms( $post_id, 'snippets_category' ); | |
$links = array(); | |
foreach ( $terms as $term ) { | |
$link = get_term_link( $term, 'snippets_category' ); | |
if ( is_wp_error( $link ) ) { | |
return $link; | |
} | |
$links[] = '<a href="' . admin_url( 'edit.php?post_type=snippets&snippets_category=' . $term->slug ) . '" rel="tag">' . $term->name . '</a>'; | |
} | |
$terms = join( ', ', $links ); | |
if ( is_string( $terms ) ) | |
echo $terms; | |
else | |
_e( 'No avialable term.', 'pine' ); | |
break; | |
} | |
} | |
add_action( 'manage_snippets_posts_custom_column' , 'custom_snippets_column', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment