admin管理员组文章数量:1125477
What I am doing is, I have to check if the post exists in any of the categories or not. I have tried below two codes but it's not working for me.
This is the first code
foreach ($scats as $key => $value) {
//echo $value->name;
if (is_object_in_term(get_the_ID(),$value->term_id)) {
echo "Found in category: " . esc_html($value->name);
}
}
This is the second code
foreach ($scats as $category) {
if (in_category($category->slug, get_the_ID())) {
echo "Found in category: " . esc_html($category->name);
}
}
I am not getting the output. Would you help me out with this?
Below is the $scats
array
Array
(
[0] => WP_Term Object
(
[term_id] => 700
[name] => Change
[slug] => change
[term_group] => 0
[term_taxonomy_id] => 700
[taxonomy] => solution_cats
[description] =>
[parent] => 0
[count] => 6
[filter] => raw
[cat_ID] => 700
[category_count] => 6
[category_description] =>
[cat_name] => Change
[category_nicename] => change
[category_parent] => 0
)
[1] => WP_Term Object
(
[term_id] => 701
[name] => Game
[slug] => game
[term_group] => 0
[term_taxonomy_id] => 701
[taxonomy] => solution_cats
[description] =>
[parent] => 0
[count] => 9
[filter] => raw
[cat_ID] => 701
[category_count] => 9
[category_description] =>
[cat_name] => Game
[category_nicename] => game
[category_parent] => 0
)
)
What I am doing is, I have to check if the post exists in any of the categories or not. I have tried below two codes but it's not working for me.
This is the first code
foreach ($scats as $key => $value) {
//echo $value->name;
if (is_object_in_term(get_the_ID(),$value->term_id)) {
echo "Found in category: " . esc_html($value->name);
}
}
This is the second code
foreach ($scats as $category) {
if (in_category($category->slug, get_the_ID())) {
echo "Found in category: " . esc_html($category->name);
}
}
I am not getting the output. Would you help me out with this?
Below is the $scats
array
Array
(
[0] => WP_Term Object
(
[term_id] => 700
[name] => Change
[slug] => change
[term_group] => 0
[term_taxonomy_id] => 700
[taxonomy] => solution_cats
[description] =>
[parent] => 0
[count] => 6
[filter] => raw
[cat_ID] => 700
[category_count] => 6
[category_description] =>
[cat_name] => Change
[category_nicename] => change
[category_parent] => 0
)
[1] => WP_Term Object
(
[term_id] => 701
[name] => Game
[slug] => game
[term_group] => 0
[term_taxonomy_id] => 701
[taxonomy] => solution_cats
[description] =>
[parent] => 0
[count] => 9
[filter] => raw
[cat_ID] => 701
[category_count] => 9
[category_description] =>
[cat_name] => Game
[category_nicename] => game
[category_parent] => 0
)
)
Share
Improve this question
edited Feb 1, 2024 at 8:32
Naren Verma
asked Feb 1, 2024 at 5:40
Naren VermaNaren Verma
2491 gold badge6 silver badges19 bronze badges
2
|
1 Answer
Reset to default 2Since you are doing this for a custom taxonomy (solution_cats
), then you should instead use has_term()
and not in_category()
which works only with the default/core category
taxonomy. So try this with your second code:
has_term( $category->slug, $category->taxonomy, get_the_ID() )
is_object_in_term()
can also be used, but your syntax was not correct – the 2nd parameter is actually the term's taxonomy and not the term ID/name/slug (which should instead be the 3rd parameter).
- The correct syntax:
is_object_in_term(get_the_ID(),$value->taxonomy,$value->term_id)
- But you used this:
is_object_in_term(get_the_ID(),$value->term_id)
本文标签: pluginsHow to check if the post exists in any of the categories
版权声明:本文标题:plugins - How to check if the post exists in any of the categories? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736667691a1946752.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
$scats
, i.e. how are you getting the categories? Can you add the code to your post/question, and are you sureget_the_ID()
is giving the correct post ID? – Sally CJ Commented Feb 1, 2024 at 7:46