How to print specific category in a loop with articles with category tree?

hi,
I have articles with a parent and children category, so in frontend i have a loop made with loop builder and when i try to print category, bricks show all category tree.
Is there a way to print only the parent or child category and not both with {post_terms_category} in text component, without using custom code or css visibility ?

Hi Paolo,
as far as I know, this is currently not possible, but you can use a custom function inside a code element or within your child theme. Something like this should work: php - get_the_terms - only top level - WordPress Development Stack Exchange

Since Bricks 1.4 it is also possible echoing custom functions using the dynamic data syntax: Dynamic Data – Bricks Academy

Best regards,
timmse

1 Like

Hello @ninmorfeo

The {post_terms_category} will print out all the terms assigned to the post and we don’t have any Bricks option to change that criteria.

If possible, I would suggest you do not assign the parent category to the article unless it is strictly necessary. When you assign a child term to an article it is already implicit that it belongs to the parent term so no need to put both. If you work using this suggestion, the initial problem disappears.

If my suggestion is not possible for you, then as @timmse says, the only solution would be to write a small function in PHP and output it using the {echo} tag or, using a shortcode.

1 Like

Snippet solution for other users:

<?php 
$categories = get_the_category();
if ( ! empty( $categories ) ) {
    echo '<a href="' . esc_url( get_category_link( $categories[0]->term_id ) ) . '">' . esc_html( $categories[0]->name ) . '</a>';
}

?>
2 Likes