Created
November 12, 2014 13:43
-
-
Save atsea/963a90bae981e4f32566 to your computer and use it in GitHub Desktop.
Wordpress Add Custom Columns
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
/** | |
* Add a Custom Column in Posts and Custom Post Types Admin Screen | |
* http://code.tutsplus.com/articles/add-a-custom-column-in-posts-and-custom-post-types-admin-screen--wp-24934 | |
*/ | |
/** | |
* Custom Post Type Snippets to make you smile | |
* https://yoast.com/custom-post-type-snippets/ | |
* Change the Software screen columns edit.php | |
*/ | |
add_filter( "manage_software_posts_columns", "change_columns" ); | |
function change_columns( $cols ) { | |
$cols = array( | |
'cb' => '<input type="checkbox" />', | |
'featured_image' => __( '', 'trans' ), | |
'title' => __( 'Title', 'trans' ), | |
'software_categories' => __( 'Categories', 'trans' ), | |
'software_platform' => __( 'Platform', 'trans' ), | |
'software_audience' => __( 'Audience', 'trans' ), | |
'status' => __( 'Status', 'trans' ), | |
'date' => __( 'Date', 'trans' ), | |
); | |
return $cols; | |
} | |
/** | |
* Add a Custom Column in Posts and Custom Post Types Admin Screen | |
* http://code.tutsplus.com/articles/add-a-custom-column-in-posts-and-custom-post-types-admin-screen--wp-24934 | |
*/ | |
// GET FEATURED IMAGE | |
function ud_get_featured_image($post_ID) { | |
$post_thumbnail_id = get_post_thumbnail_id($post_ID); | |
if ($post_thumbnail_id) { | |
$post_thumbnail_img = wp_get_attachment_image_src($post_thumbnail_id, 'featured_preview'); | |
return $post_thumbnail_img[0]; | |
} | |
} | |
// Display column content | |
add_action( "manage_posts_custom_column", "ud_software_custom_columns", 10, 2 ); | |
function ud_software_custom_columns( $column, $post_id ) { | |
switch ( $column ) { | |
case "featured_image": | |
$featured_image = ud_get_featured_image($post_ID); | |
if($featured_image) { | |
echo '<img width="30" height="37" src="' . $featured_image . '">'; | |
} | |
else { | |
echo '<img width="30" height="30" src="' .get_stylesheet_directory_uri(). '/images/icon-default.png">'; | |
} | |
break; | |
case "title": | |
$title = get_post_meta( $post_id, 'title', true); | |
echo '<a href="' . $title . '">' . $title. '</a>'; | |
break; | |
case "status": | |
$show_custom_state = get_post_meta( $post_id, '_status', true); | |
if(!empty ($show_custom_state)){ | |
echo '<span class="custom_state ' . strtolower( $show_custom_state ) . '">' . $show_custom_state . '</span>'; | |
/** | |
*wp_set_object terms won't remove all terms | |
* http://wordpress.stackexchange.com/questions/29750/wp-set-object-terms-wont-remove-all-terms | |
* Clear Categorie is status is inactive | |
*/ | |
wp_set_object_terms( $post_id, NULL, 'software_categories' ); | |
} | |
else { | |
echo '<span class="custom_state_none">Active</span>'; | |
} | |
//echo get_post_meta( $post_id, '_status', true); | |
break; | |
case "software_categories": | |
$terms_categories = get_the_terms($post->ID, 'software_categories', true); | |
if(!empty ($terms_categories)) { | |
foreach ( $terms_categories as $term ) { | |
$term_categories_link = get_term_link( $term, 'software_categories' ); | |
echo '<a href="'. $term_categories_link.'">' .$term->name .'</a>'; | |
} | |
} | |
break; | |
case "software_platform": | |
$terms_platform = get_the_terms($post->ID, 'software_platform', true); | |
if(!empty ($terms_platform)) { | |
foreach ( $terms_platform as $term ) { | |
$term_platform_link = get_term_link( $term, 'software_platform' ); | |
$out[] = '<a href="'. $term_platform_link.'">' .$term->name .'</a>'; | |
} | |
echo join( ', ', $out ); | |
} | |
break; | |
case "software_audience": | |
$terms_audience = get_the_terms($post->ID, 'software_audience', true); | |
if(!empty ($terms_audience)) { | |
foreach ( $terms_audience as $term ) { | |
$term_audience_link = get_term_link( $term, 'software_audience' ); | |
$out[] = '<a href="'. $term_audience_link.'">' .$term->name .'</a>'; | |
} | |
echo join( ', ', $out ); | |
} | |
break; | |
case "date": | |
echo the_date( $post_id); | |
break; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment