首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >注册Taxonomy Wordpress -定制列

注册Taxonomy Wordpress -定制列
EN

Stack Overflow用户
提问于 2018-09-04 23:33:21
回答 1查看 227关注 0票数 0

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

我的代码:

代码语言:javascript
复制
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“,以在我的管理自定义列中显示?

提前谢谢你。

致以亲切的问候。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-09-05 00:00:05

您需要为分类法添加另一个案例

代码语言:javascript
复制
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;
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52169779

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档