admin管理员组文章数量:1394228
I have the following data coming from my WP Rest API via .php/wp-json/wp/v2/taxonomies
:
{
"category": {
"name": "Categories",
"slug": "category",
"description": "",
"types": [
"post"
],
"hierarchical": true,
"rest_base": "categories",
"_links": {
"collection": [
{
"href": ".php/wp-json/wp/v2/taxonomies"
}
],
"wp:items": [
{
"href": ".php/wp-json/wp/v2/categories"
}
],
"curies": [
{
"name": "wp",
"href": "/{rel}",
"templated": true
}
]
}
},
"post_tag": {
"name": "Tags",
"slug": "post_tag",
"description": "",
"types": [
"post"
],
"hierarchical": false,
"rest_base": "tags",
"_links": {
"collection": [
{
"href": ".php/wp-json/wp/v2/taxonomies"
}
],
"wp:items": [
{
"href": ".php/wp-json/wp/v2/tags"
}
],
"curies": [
{
"name": "wp",
"href": "/{rel}",
"templated": true
}
]
}
},
"dog": {
"name": "Dogs",
"slug": "dog",
"description": "",
"types": [
"poodle",
"labrador",
"beagle",
"retriever"
],
"hierarchical": false,
"rest_base": "dog",
"_links": {
"collection": [
{
"href": ".php/wp-json/wp/v2/taxonomies"
}
],
"wp:items": [
{
"href": ".php/wp-json/wp/v2/dog"
}
],
"curies": [
{
"name": "wp",
"href": "/{rel}",
"templated": true
}
]
}
}
}
I am trying to retrieve the array of dog types in a custom plugin.
[
"poodle",
"labrador",
"beagle",
"retriever"
]
How do I access the above array via wordpress hooks? I have tried the following with no luck:
get_taxonomy('dog')->types;
$wp_taxonomies['dog']->types;
Here is the code I am trying to implement in a custom plugin:
$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;
}
}
I have the following data coming from my WP Rest API via https://cms.dboxcg/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/index.php/wp-json/wp/v2/taxonomies"
}
],
"wp:items": [
{
"href": "https://cms.dboxcg/index.php/wp-json/wp/v2/categories"
}
],
"curies": [
{
"name": "wp",
"href": "https://api.w/{rel}",
"templated": true
}
]
}
},
"post_tag": {
"name": "Tags",
"slug": "post_tag",
"description": "",
"types": [
"post"
],
"hierarchical": false,
"rest_base": "tags",
"_links": {
"collection": [
{
"href": "https://cms.dboxcg/index.php/wp-json/wp/v2/taxonomies"
}
],
"wp:items": [
{
"href": "https://cms.dboxcg/index.php/wp-json/wp/v2/tags"
}
],
"curies": [
{
"name": "wp",
"href": "https://api.w/{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/index.php/wp-json/wp/v2/taxonomies"
}
],
"wp:items": [
{
"href": "https://cms.dboxcg/index.php/wp-json/wp/v2/dog"
}
],
"curies": [
{
"name": "wp",
"href": "https://api.w/{rel}",
"templated": true
}
]
}
}
}
I am trying to retrieve the array of dog types in a custom plugin.
[
"poodle",
"labrador",
"beagle",
"retriever"
]
How do I access the above array via wordpress hooks? I have tried the following with no luck:
get_taxonomy('dog')->types;
$wp_taxonomies['dog']->types;
Here is the code I am trying to implement in a custom plugin:
$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;
}
}
Share
Improve this question
edited Apr 3, 2020 at 23:51
Joel Hoelting
asked Apr 3, 2020 at 22:26
Joel HoeltingJoel Hoelting
1375 bronze badges
4
|
1 Answer
Reset to default 1You're using the correct code (get_taxonomy('dog')->object_type
) or function, but it should only be used after the taxonomy has been registered. Otherwise, the function would return a false
instead of a proper taxonomy object which contains properties like object_type
which is an array of post types registered for the taxonomy.
And because taxonomies are normally (or should be) registered during the init
hook, you can use any hooks that WordPress fires after the init
hook, e.g. wp_loaded
or in your case, you'd want to use the admin_init
which runs on the admin side of the site like the post editing screens:
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 );
}
} );
本文标签: How do I get array of types associated with a taxonomy
版权声明:本文标题:How do I get array of types associated with a taxonomy? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744600349a2615031.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
get_taxonomy('dog')->object_type
. But if you mean retrieving it from that JSON response, then how are you retrieving it? Via PHP, JS? – Sally CJ Commented Apr 3, 2020 at 22:47get_taxonomy('dog')->object_type
but I am getting the following error:Notice: Trying to get property 'object_type' of non-object
. – Joel Hoelting Commented Apr 3, 2020 at 23:32