Created
July 29, 2012 15:12
-
-
Save ScottPhillips/3199420 to your computer and use it in GitHub Desktop.
Add Custom Columns to WordPress Post List Page
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 | |
add_filter( 'manage_posts_columns', 'your_custom_cols' ); //Filter out Post Columns with 2 custom columns | |
function your_custom_cols($defaults) { | |
$defaults['language'] = __('Language'); //Language and Films is name of column | |
$defaults['film'] = __('Films'); | |
return $defaults; | |
} | |
add_action('manage_posts_custom_column', 'custom_cols_data', 10, 2); //Just need a single function to add multiple columns | |
function custom_cols_data($column_name, $post_id) { | |
global $wpdb; | |
if( $column_name == 'language' ) { | |
$tags = get_the_terms($post->ID, 'lang'); //lang is the first custom taxonomy slug | |
if ( !empty( $tags ) ) { | |
$out = array(); | |
foreach ( $tags as $c ) | |
$out[] = "<a href='edit.php?lang=$c->slug'> " . esc_html(sanitize_term_field('name', $c->name, $c->term_id, 'lang', 'display')) . "</a>"; | |
echo join( ', ', $out ); | |
} else { | |
_e('No Languages.'); //No Taxonomy term defined | |
} | |
} else if( $column_name == 'film' ) { //Adding 2nd column | |
$tags = get_the_terms($post->ID, 'film'); //film is the next taxonomy slug for column Films | |
if ( !empty( $tags ) ) { | |
$out = array(); | |
foreach ( $tags as $c ) | |
$out[] = "<a href='edit.php?film=$c->slug'> " . esc_html(sanitize_term_field('name', $c->name, $c->term_id, 'film', 'display')) . "</a>"; | |
echo join( ', ', $out ); | |
} else { | |
_e('No Related Films.'); //no Films found | |
} | |
} else { | |
echo '<i>'.__('None').'</i>'; //Only 2 columns, blank now. | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment