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

categories - Show category in post page, that is in specific category

programmeradmin1浏览0评论

In my WordPress categories section, I've a Food main category, with other sub-categories ("hamburger, pasta, main, etc"). In post page i want to show only the child categories that are filled in the post.

I want to show only child category selected. If the post is in Food > hamburger and Food > main, i want to show only these 2 categories on post page, instead of all categories. I tried with this code, but it will show all categories:

$categories = get_categories( array(
    'taxonomy' => 'category', 
    'orderby' => 'name',
    'order' => 'ASC',
    'hide_empty' => true, 
    'include' => 'all',
    'exclude' => '', 
    'exclude_tree' => 'all', 
    'number' => false,
    'fields' => 'all',
    'name' => '',
    'slug' => '',
    'hierarchical' => true,
    'search' => '',
    'name__like' => '',
    'description__like' => '',
    'pad_counts' => false,
    'get' => '',
    'child_of' => false,
    'childless' => false,
    'cache_domain' => 'core',
    'update_term_meta_cache' => true,
    'meta_query' => '',
    'meta_key' => array(),
    'meta_value'=> ''
));

foreach ( $categories as $category ) {
    printf( '<a href="%1$s">%2$s</a><br />',
        esc_url( get_category_link( $category->term_id ) ),
        esc_html( $category->name )
    );
}

In my WordPress categories section, I've a Food main category, with other sub-categories ("hamburger, pasta, main, etc"). In post page i want to show only the child categories that are filled in the post.

I want to show only child category selected. If the post is in Food > hamburger and Food > main, i want to show only these 2 categories on post page, instead of all categories. I tried with this code, but it will show all categories:

$categories = get_categories( array(
    'taxonomy' => 'category', 
    'orderby' => 'name',
    'order' => 'ASC',
    'hide_empty' => true, 
    'include' => 'all',
    'exclude' => '', 
    'exclude_tree' => 'all', 
    'number' => false,
    'fields' => 'all',
    'name' => '',
    'slug' => '',
    'hierarchical' => true,
    'search' => '',
    'name__like' => '',
    'description__like' => '',
    'pad_counts' => false,
    'get' => '',
    'child_of' => false,
    'childless' => false,
    'cache_domain' => 'core',
    'update_term_meta_cache' => true,
    'meta_query' => '',
    'meta_key' => array(),
    'meta_value'=> ''
));

foreach ( $categories as $category ) {
    printf( '<a href="%1$s">%2$s</a><br />',
        esc_url( get_category_link( $category->term_id ) ),
        esc_html( $category->name )
    );
}
Share Improve this question edited May 3, 2019 at 13:14 Alexander Holsgrove 1,9091 gold badge15 silver badges25 bronze badges asked May 3, 2019 at 13:09 davi90ctdavi90ct 33 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You should use get_the_category() instead of get_categories().

The get_the_category() function will return only categories assigned to the post, while get_categories() that you use returns all existing categories.

Edit:

To get categories assigned to post, but only those with the ancestor Food, you can use code:

$args = [
    'object_ids' => get_the_ID(),   // only categories assigned to current post
    'child_of' => {ID_of_food_cat}  // limit results to children (descendants) of this category
];
$cats = get_categories($args);

ID of parent category can be hard-coded or dynamic:

$food_cat_id = get_category_by_slug('food');  // <-- slug of Food
$food_cat_id = ($food_cat_id instanceof WP_Term) ? $food_cat_id->term_id : 0;
发布评论

评论列表(0)

  1. 暂无评论