One of the dynamic data is {post_terms_category} that should list all the categories of a post id. By default each category is wrapped with a html <a>
tag with the href of the category. Is there a way to output the category->name or category->slug only?
I fixed this issue by creating my own function and then use {echo:my_own_function}:
function my_own_function(){
$id = get_the_ID();
$categories = get_the_category($id);
$output = '';
$loop = 0;
foreach($categories as $cat){
if($loop === 0){
$output .= $cat->name;
$loop++;
} else {
$output .= ', ' . $cat->name;
}
}
return $output;
}
This is awesome!
I really like how you dealt with the comma’s . I usually add them on the end and then remove one but this is brilliant.
Thanks! Appreciate it.
I hope there will be a core function in future to replace this workaround.
Here’s a one-liner for the same:
echo strip_tags( get_the_term_list( get_the_ID(), 'category', '', ', ' ) );
Great. Did you try to run it as a {echo:function} ?
Yes and it works fine.
function bl_unlinked_categories() {
return strip_tags( get_the_term_list( get_the_ID(), 'category', '', ', ' ) );
}
Usage:
{echo:bl_unlinked_categories}
I meant: {echo:strip_tags( get_the_term_list( get_the_ID(), ‘category’, ‘’, ', ’ ) )}
That won’t work since we can’t pass functions as arguments as far as I know. Better confirm with Bricks support.
thanks, good to know.
I confirm, tried this kind of nested functions, doesn’t work.
I have another version for post categories, less consuming, I guess, no need to strip, just get the names:
implode(',', wp_get_post_categories( get_the_ID(), array( 'fields' => 'names' ) ))
Also works with 'fields' => 'ids'
(very handy for using in tax query), or you can get all term object as well if you need the whole stuff.
works great, thanks!
Hello, how wrapping it ?
for example with
<ul class="">
<li>term 1</li>
<li>term 2</li>
<li>…</li>
</ul>
Any help appreciated.
Best regard