如何获取与分类相关联的类型数组?

时间:2020-04-03 作者:Joel Hoelting

我有以下数据来自我的WP Rest API,通过https://cms.dboxcg.com/index.php/wp-json/wp/v2/taxonomies:

{
"category": {
    "name": "Categories",
    "slug": "category",
    "description": "",
    "types": [
        "post"
    ],
    "hierarchical": true,
    "rest_base": "categories",
    "_links": {
        "collection": [
            {
                "href": "https://cms.dboxcg.com/index.php/wp-json/wp/v2/taxonomies"
            }
        ],
        "wp:items": [
            {
                "href": "https://cms.dboxcg.com/index.php/wp-json/wp/v2/categories"
            }
        ],
        "curies": [
            {
                "name": "wp",
                "href": "https://api.w.org/{rel}",
                "templated": true
            }
        ]
    }
},
"post_tag": {
    "name": "Tags",
    "slug": "post_tag",
    "description": "",
    "types": [
        "post"
    ],
    "hierarchical": false,
    "rest_base": "tags",
    "_links": {
        "collection": [
            {
                "href": "https://cms.dboxcg.com/index.php/wp-json/wp/v2/taxonomies"
            }
        ],
        "wp:items": [
            {
                "href": "https://cms.dboxcg.com/index.php/wp-json/wp/v2/tags"
            }
        ],
        "curies": [
            {
                "name": "wp",
                "href": "https://api.w.org/{rel}",
                "templated": true
            }
        ]
    }
},
"dog": {
    "name": "Dogs",
    "slug": "dog",
    "description": "",
    "types": [
        "poodle",
        "labrador",
        "beagle",
        "retriever"
    ],
    "hierarchical": false,
    "rest_base": "dog",
    "_links": {
        "collection": [
            {
                "href": "https://cms.dboxcg.com/index.php/wp-json/wp/v2/taxonomies"
            }
        ],
        "wp:items": [
            {
                "href": "https://cms.dboxcg.com/index.php/wp-json/wp/v2/dog"
            }
        ],
        "curies": [
            {
                "name": "wp",
                "href": "https://api.w.org/{rel}",
                "templated": true
            }
        ]
    }
}
}

我正在试图检索自定义插件中的狗类型数组。

[
    "poodle",
    "labrador",
    "beagle",
    "retriever"
]
如何通过wordpress挂钩访问上述数组?我尝试了以下方法,但没有成功:

get_taxonomy(\'dog\')->types;
$wp_taxonomies[\'dog\']->types;
下面是我试图在自定义插件中实现的代码:

$dog_types = get_taxonomy(\'dog\')->object_type;

foreach ($dog_types as $type) {
    add_filter( "manage_{$type}_posts_columns", \'update_dog_type_columns\' );
    add_action( "manage_{$type}_posts_custom_column", \'update_dog_type_column\', 10, 2 );
}

function update_dog_type_columns( $columns ) {
    $columns = array(
        \'cb\' => $columns[\'cb\'],
        \'title\' => __( \'Title\' ),
        \'image\' => __( \'Thumbnail\' ),
        \'date\' => __( \'Date\' )
    );

    return $columns;
}

function update_dog_type_column( $column, $post_id ) {
    switch ( $column ) {
    case \'image\':
        $vimeo_link = get_field(\'vimeo_link\');
        if ($vimeo_link) {
            $vimeo_logo_url = \'https://imageurl/vimeo_logo.jpg\';
            echo \'<img src="\' . $vimeo_logo_url . \'" height="100px" width="100px" />\';
        break;
        } else {
            $img_array = get_field(\'image\');
            $img = $img_array[\'sizes\'][\'thumbnail\'];
            echo \'<img src="\' . $img . \'" height="100px" width="100px" />\';
            break;
        }
    case \'year\':
        echo get_field( \'year\', $post_id );
        break;
    }
}

1 个回复
最合适的回答,由SO网友:Sally CJ 整理而成

您使用的代码正确(get_taxonomy(\'dog\')->object_type) 或function, 但只有在分类法注册后才能使用它。否则,函数将返回false 而不是正确的分类法object 包含以下属性object_type 这是为分类法注册的帖子类型数组。

由于分类法通常(或应该)在init 钩子,您可以使用WordPress在init 挂钩,例如。wp_loaded 或者在你的情况下,你想使用admin_init 在站点的管理端运行,如后期编辑屏幕:

add_action( \'admin_init\', function () {
    // Make sure the taxonomy exists.
    if ( ! $tax = get_taxonomy( \'dog\' ) ) {
        return;
    }

    foreach ( $tax->object_type as $type ) {
        add_filter( "manage_{$type}_posts_columns", \'update_dog_type_columns\' );
        add_action( "manage_{$type}_posts_custom_column", \'update_dog_type_column\', 10, 2 );
    }
} );

相关推荐

Wp_Term_Taxonomy.Parent引用了什么?

我最初的想法是,它引用了另一种分类法(即wp_term_taxonomy.parent 到wp_term_taxonomy.term_taxonomy_id). Corcel这样的怪物built under this assumption. 然而,我done some digging 大多数信息似乎表明它实际上引用了wp_terms.term_id. 在Corcel的案例中,我认为这种巧合是可行的——WordPress将设置term_id 以及term_taxonomy_id 到相同的值。如果您弄乱了数据