创建自定义帖子类型和自定义分类法后,我想在自定义列中调用该自定义分类法名称,但是我尝试的所有操作都给了我空白信息。
我的代码:
function news_custom_type() {
$labels = array (
'name' => 'News',
'singular_name' => 'New',
'menu_name' => 'News',
'name_admin_bar' => 'News',
);
$args = array (
'labels' => $labels,
'show_ui' => true,
'show_in_menu' => true,
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => 26,
'menu_icon' => 'dashicons-welcome-write-blog',
'supports' => array('title', 'editor')
);
register_post_type('news', $args);
register_taxonomy(
'news_categories',
'news',
array(
'labels' => array(
'name' => 'Years',
'add_new_item' => 'Add Year',
'new_item_name' => "New Year"
),
'show_ui' => true,
'show_tagcloud' => false,
'hierarchical' => true,
'hasArchive' => true,
'show_admin_column' => true,
)
);
}
add_action( 'init', 'news_custom_type');
add_filter( 'manage_news_posts_columns', 'set_news_columns' );
add_action( 'manage_news_posts_custom_column', 'news_custom_column', 10, 2);
function set_news_columns($columns) {
$newColumns = array();
$newColumns['title'] = 'Name';
$newColumns['news_categories'] = 'Year';
//$newColumns['zzz'] = 'zzz';
$newColumns['text'] = 'Text';
$newColumns['date'] = 'Date';
return $newColumns;
}
function news_custom_column($column, $post_id) {
switch ($column) {
case 'text' :
echo get_the_excerpt();
break;
}
}
有了这个我可以得到,
https://i.stack.imgur.com/63eiG.png
https://i.stack.imgur.com/PzTxp.png
我如何称呼分类法(在我的情况下为“年”)以显示在管理自定义列中?
先谢谢您。
亲切的问候。
答案 0 :(得分:0)
您需要为分类法添加另一种情况
function news_custom_column($column, $post_id) {
switch ($column) {
case 'text' :
echo get_the_excerpt();
break;
case 'news_categories':
$terms = wp_get_post_terms( $post_id, 'news_categories' );
if ( $terms ) {
foreach ( $terms as $term ) {
if ( ! next( $terms ) ) {
echo $term->name;
} else {
echo $term->name . ', ';
}
}
}
break;
}
}