【问题标题】:Get the terms of a custom taxonomy of custom post type in an array获取数组中自定义帖子类型的自定义分类的术语
【发布时间】:2014-09-04 09:49:34
【问题描述】:

我有一个自定义帖子类型“事件”和一个分类-“事件类型”。 如何在数组中获取属于自定义分类“事件类型”的所有术语。我使用 wp_list_categories($args) 但它给出了 ali 标签中每种类型的输出。

请帮帮我

注册分类代码

<?php
function event_init() {
    // create a new taxonomy
    register_taxonomy(
        'Event types',
        'events',       
        array(
            'labels' => array(
                            'name'=>'Event types',
                            'add_new_item'=>'Add New Event types ',
                            'new_item_name'=>"New Event types"              
                            ),

            'show_ui' => true,
            'show_tagcloud' => false,
            'hierarchical' => true,
            'rewrite' => array( 'slug' => 'event-type' ),



             )
    );
}
add_action( 'init', 'event_init' );

?>

【问题讨论】:

  • “我怎样才能在一个数组中获取所有类型的“事件类型”分类法”。 “获取所有类型”到底是什么意思?您的意思是获取属于“事件类型”分类的所有术语吗?或者您的意思是从“事件类型”分类中获取所有标记有术语的帖子?你需要具体。
  • 对不起,我想要所有属于“事件类型”分类的术语
  • 请添加您如何注册您的分类的代码,因为您在这里用您的分类名称混淆了每个人
  • 我已经添加了上面的代码

标签: php wordpress custom-post-type custom-taxonomy


【解决方案1】:

$terms = get_terms('事件类型');

这对你有用。为了获得更好的体验,请勿在分类名称或任何其他变量名称中使用大写字母和空格。

参考:https://developer.wordpress.org/reference/functions/get_terms/

【讨论】:

    【解决方案2】:
    $terms = get_terms( 'your_taxonomy_name' );
    

    这将为您提供一系列术语对象。 注意您的分类名称只能是小写字母和下划线。

    参考:http://codex.wordpress.org/Function_Reference/get_terms

    【讨论】:

    • 我在数组对象(WP_Error)#324 (2) { ["errors"]=> array(1) { ["invalid_taxonomy"]=> array(1) { [0 ]=> string(16) "无效分类法" } } ["error_data"]=> array(0) { } }
    • 您确定您的分类名称是“事件类型”吗? 另请注意:当您注册您的分类时,您实际上应该确保使用所有小写字符。见here
    • 自定义分类的名称是正确的。但这是获取自定义帖子类型的自定义分类的术语对象的代码吗?
    • 您收到错误“invalid_taxonomy” - 您确定 Event-types 是您的分类的确切名称吗?
    • 实际上,Event-types 不可能是您的分类名称,因为 WordPress 不允许在自定义分类名称中使用 - 字符。
    猜你喜欢
    • 1970-01-01
    • 2018-01-08
    • 2016-07-01
    • 2016-11-10
    • 1970-01-01
    • 1970-01-01
    • 2016-10-28
    • 1970-01-01
    • 2019-11-04
    相关资源
    最近更新 更多