注册分类Wordpress - 自定义列

问题描述 投票:0回答:1

在创建自定义帖子类型和自定义分类后,我想在自定义列中调用自定义分类名称,但我尝试的所有内容都只给了我空白信息。

我的代码:

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

如何在我的案例“年”中调用分类法,以显示在我的管理员自定义列中?

先感谢您。

亲切的问候。

php wordpress custom-post-type custom-taxonomy
1个回答
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;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.