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

Include custom post type custom taxonomies in Categories widget

programmeradmin0浏览0评论

I wanted to see if this was possible, but I want to include the custom post type custom taxonomy categories inside the WordPress Categories widget.

I know that there is a filter as shown below:

add_filter('widget_categories_args', function() {
    $params['post_type'] = array('post', 'recipe');
    return $params;
});
  1. My taxonomy is called recipe-categories inside wp_term_taxonomy and I want to be able to pull in all the categories inside the Categories Widget.

  2. recipe is my custom post type.

  3. Url string is taxonomy=recipe-categories&post_type=recipe

Here are all the categories that I have:

All that I'm getting back is the post categories and not my recipe categories as shown below:

I wanted to see if this was possible, but I want to include the custom post type custom taxonomy categories inside the WordPress Categories widget.

I know that there is a filter as shown below:

add_filter('widget_categories_args', function() {
    $params['post_type'] = array('post', 'recipe');
    return $params;
});
  1. My taxonomy is called recipe-categories inside wp_term_taxonomy and I want to be able to pull in all the categories inside the Categories Widget.

  2. recipe is my custom post type.

  3. Url string is taxonomy=recipe-categories&post_type=recipe

Here are all the categories that I have:

All that I'm getting back is the post categories and not my recipe categories as shown below:

Share Improve this question asked Feb 16, 2020 at 4:36 DevSemDevSem 2092 silver badges11 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

The widget class uses wp_list_categories() which supports all the parameters for get_terms() like taxonomy which you can use to make the widget lists categories in a taxonomy other than category.

So your code would be:

add_filter('widget_categories_args', function( $params ) {
//  $params['post_type'] = array('post', 'recipe'); // post_type is not a standard parameter for get_terms()
    $params['taxonomy'] = 'recipe-categories';
    return $params;
});

However, the widget only supports a single taxonomy because wp_list_categories() uses taxonomy_exists() which doesn't support multiple taxonomies. Here's the relevant code in wp_list_categories() which causes the limitation:

if ( ! taxonomy_exists( $parsed_args['taxonomy'] ) ) {
    return false;
}

So if you want to use multiple taxonomies, then you'd need a plugin for that (i.e. one that provides a similar widget) or you can code your own widget class.

发布评论

评论列表(0)

  1. 暂无评论