admin管理员组文章数量:1130349
I have written my custom WP_Query and using loop to display post content. I use get_the_category() to display categories of current post and it works fine. Now for some post types there are custom taxonomies instead of categories.
Code to get categories:
$categories = get_the_category();
if(!empty($categories)){
foreach($categories as $index => $cat){
echo $cat->name;
}
}
Now I need to pull all taxonomies and print them in comma separated format.
I tried this:
$taxonomies = get_the_taxonomies();
if(!empty($taxonomies)){
foreach($taxonomies as $taxonomy){
echo $taxonomy;
}
}
It works and shows in this format "Taxonomy Label: Term (hyperlinked)". If the terms are more than one than it adds "and" between terms. I need only terms and if they are multiple then they should be separated by comma.
I want to know:
- The best approach to achieve those results
- Is it recommended to to use above method?
- Can I use
regexto extract value? - How can I get rid off hyperlink?
Thanks
I have written my custom WP_Query and using loop to display post content. I use get_the_category() to display categories of current post and it works fine. Now for some post types there are custom taxonomies instead of categories.
Code to get categories:
$categories = get_the_category();
if(!empty($categories)){
foreach($categories as $index => $cat){
echo $cat->name;
}
}
Now I need to pull all taxonomies and print them in comma separated format.
I tried this:
$taxonomies = get_the_taxonomies();
if(!empty($taxonomies)){
foreach($taxonomies as $taxonomy){
echo $taxonomy;
}
}
It works and shows in this format "Taxonomy Label: Term (hyperlinked)". If the terms are more than one than it adds "and" between terms. I need only terms and if they are multiple then they should be separated by comma.
I want to know:
- The best approach to achieve those results
- Is it recommended to to use above method?
- Can I use
regexto extract value? - How can I get rid off hyperlink?
Thanks
Share Improve this question asked Nov 18, 2018 at 10:21 AlenaAlena 1378 bronze badges 1 |2 Answers
Reset to default 2The first problem with your code, I guess, is that you use get_the_taxonomies function, which will:
Retrieve all taxonomies of a post with just the names.
So its result will be like this:
Array
(
[0] => category
[1] => post_tag
[2] => post_format
)
And I'm pretty sure that you want to get terms assigned to given post from all taxonomies, and not the taxonomies names...
So most probably you want to do something like this:
$terms = wp_get_object_terms( get_the_ID(), array_keys( get_the_taxonomies() ) );
foreach ( $terms as $i => $term ) {
echo ($i ? ', ' : '') . $term->name;
}
And quick answers to your questions:
- One of possible solutions above - it's hard to say if it's the best one.
- No, your method is not a solution, I guess.
- There is no need to use regex. You should avoid using regex when there is no need for that.
- You can get rid of hyperlinks by getting term objects and printing them by yourself (as shown above).
Take a look at these:
$taxonomies = get_post_taxonomies( );
print_r( $taxonomies );
echo implode( $taxonomies, ', ' );
I have written my custom WP_Query and using loop to display post content. I use get_the_category() to display categories of current post and it works fine. Now for some post types there are custom taxonomies instead of categories.
Code to get categories:
$categories = get_the_category();
if(!empty($categories)){
foreach($categories as $index => $cat){
echo $cat->name;
}
}
Now I need to pull all taxonomies and print them in comma separated format.
I tried this:
$taxonomies = get_the_taxonomies();
if(!empty($taxonomies)){
foreach($taxonomies as $taxonomy){
echo $taxonomy;
}
}
It works and shows in this format "Taxonomy Label: Term (hyperlinked)". If the terms are more than one than it adds "and" between terms. I need only terms and if they are multiple then they should be separated by comma.
I want to know:
- The best approach to achieve those results
- Is it recommended to to use above method?
- Can I use
regexto extract value? - How can I get rid off hyperlink?
Thanks
I have written my custom WP_Query and using loop to display post content. I use get_the_category() to display categories of current post and it works fine. Now for some post types there are custom taxonomies instead of categories.
Code to get categories:
$categories = get_the_category();
if(!empty($categories)){
foreach($categories as $index => $cat){
echo $cat->name;
}
}
Now I need to pull all taxonomies and print them in comma separated format.
I tried this:
$taxonomies = get_the_taxonomies();
if(!empty($taxonomies)){
foreach($taxonomies as $taxonomy){
echo $taxonomy;
}
}
It works and shows in this format "Taxonomy Label: Term (hyperlinked)". If the terms are more than one than it adds "and" between terms. I need only terms and if they are multiple then they should be separated by comma.
I want to know:
- The best approach to achieve those results
- Is it recommended to to use above method?
- Can I use
regexto extract value? - How can I get rid off hyperlink?
Thanks
Share Improve this question asked Nov 18, 2018 at 10:21 AlenaAlena 1378 bronze badges 1-
Do you want to print taxonomies or terms?
get_the_taxonomieswill get you taxonomies (like: category, post_tag, post_format) and not terms from that taxonomies... – Krzysiek Dróżdż Commented Nov 18, 2018 at 11:31
2 Answers
Reset to default 2The first problem with your code, I guess, is that you use get_the_taxonomies function, which will:
Retrieve all taxonomies of a post with just the names.
So its result will be like this:
Array
(
[0] => category
[1] => post_tag
[2] => post_format
)
And I'm pretty sure that you want to get terms assigned to given post from all taxonomies, and not the taxonomies names...
So most probably you want to do something like this:
$terms = wp_get_object_terms( get_the_ID(), array_keys( get_the_taxonomies() ) );
foreach ( $terms as $i => $term ) {
echo ($i ? ', ' : '') . $term->name;
}
And quick answers to your questions:
- One of possible solutions above - it's hard to say if it's the best one.
- No, your method is not a solution, I guess.
- There is no need to use regex. You should avoid using regex when there is no need for that.
- You can get rid of hyperlinks by getting term objects and printing them by yourself (as shown above).
Take a look at these:
$taxonomies = get_post_taxonomies( );
print_r( $taxonomies );
echo implode( $taxonomies, ', ' );
本文标签: wp queryGetting Taxonomy inside WPQuery Loop
版权声明:本文标题:wp query - Getting Taxonomy inside WP_Query Loop 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749171903a2327152.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


get_the_taxonomieswill get you taxonomies (like: category, post_tag, post_format) and not terms from that taxonomies... – Krzysiek Dróżdż Commented Nov 18, 2018 at 11:31