注册分类法 Wordpress - 自定义列
Register Taxonomy Wordpress - Custom Columns
创建自定义 post 类型和自定义分类后,我想在我的自定义列中调用自定义分类名称,但我尝试的所有操作都给我空白信息。
我的代码:
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
如何调用分类法,在我的例子中 "years" 显示在我的管理员自定义列中?
提前谢谢你。
亲切的问候。
您需要为分类法添加另一个案例
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;
}
}
创建自定义 post 类型和自定义分类后,我想在我的自定义列中调用自定义分类名称,但我尝试的所有操作都给我空白信息。
我的代码:
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
如何调用分类法,在我的例子中 "years" 显示在我的管理员自定义列中?
提前谢谢你。
亲切的问候。
您需要为分类法添加另一个案例
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;
}
}