admin管理员组文章数量:1022769
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
1 Answer
Reset to default 0You 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;
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
1 Answer
Reset to default 0You 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;
本文标签: categoriesShow category in post pagethat is in specific category
版权声明:本文标题:categories - Show category in post page, that is in specific category 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745525843a2154519.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论