最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How do I get array of types associated with a taxonomy?

programmeradmin1浏览0评论

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 Well, as I said on your previous question, you can use 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:47
  • Hi! Ideally I would get the array directly on the backend via PHP. I tried to use get_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
  • 1 What hook did you use? Could you please add your code to the question? – Sally CJ Commented Apr 3, 2020 at 23:36
  • Thanks for your help, I added the code above I am trying to get working. – Joel Hoelting Commented Apr 3, 2020 at 23:46
Add a comment  | 

1 Answer 1

Reset to default 1

You'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 );
    }
} );
发布评论

评论列表(0)

  1. 暂无评论