- This topic has 5 replies, 2 voices, and was last updated 3 years, 3 months ago by
David.
-
AuthorPosts
-
January 31, 2023 at 3:52 pm #2516400
Utopia – Arquitectura e Engenharia Lda
Hi,
I am customizing archive pages and I am stuck with a button displaying categories for each post.
I know how to create a button that displays the categories of each post.
Then I select the block and choose:
DYNAMIC TEXT TYPE – List of terms
TAXONOMY – category
DYNAMIC LINK TYPE – Term archivesThe problem is that I have archives for parent categories and for child subcategories.
So, my question is
1)
How can I exclude from the list of terms displayed on the button parent categories on child archives?
2)
Similarly I want to exclude child list of terms on parent category archives.Thanks.
February 1, 2023 at 9:03 am #2517263David
StaffCustomer SupportHi there,
just to be clear, you do want to change what posts are displayed.. just the terms that each post has ?
February 1, 2023 at 2:57 pm #2517657Utopia – Arquitectura e Engenharia Lda
Hi David,
Thank you very much for your answer.
I just want to change the terms that are displayed.February 2, 2023 at 3:52 am #2518344David
StaffCustomer SupportOk, so it requires a custom function.
1. Add this PHP Snippet to your site:// display custom terms in loop // show only parent terms on parent archives // show only child terms on child archives function custom_get_terms() { // set the taxonomy $tax_name = 'category'; // is archive parent or child logic $current_term = get_queried_object(); $is_parent = $current_term->parent == 0 ? true : false; // get the terms $terms = get_the_terms( get_the_ID(), $tax_name ); $html = '<ul class="custom-terms">'; foreach( $terms as $key => $term ) { if ( ( $is_parent && $term->parent == 0 ) || ( !$is_parent && $term->parent != 0 ) ) { // return html if term heirarchy matches archives $html .= '<li><a href="' . esc_attr( get_term_link( $term->slug, $tax_name ) ) . '">' . __( $term->name ) . '</a></li>'; } } $html .= '</ul>'; return $html; } // render_block with class of custom-terms add_filter( 'render_block', function( $block_content, $block ) { if ( ! empty( $block['attrs']['className'] ) && 'custom-terms' === $block['attrs']['className'] ) { $block_content = custom_get_terms(); } return $block_content; }, 10, 2 );2.Then in your element, add a headline block, with just some static text, and in Advanced > Additional CSS Class(es) add:
custom-terms3. It will require some CSS to remove bullets and display it as a row:
.custom-terms { list-style: none; margin-left: 0; display: flex; flex-wrap: wrap; gap: 5px; }The font size and colors you can apply directly to the headline block.
February 2, 2023 at 2:51 pm #2519099Utopia – Arquitectura e Engenharia Lda
Absolutly perfect!.
You guys are amazing!
Sincere thanks!February 3, 2023 at 2:30 am #2519753David
StaffCustomer SupportGlad to be of help
-
AuthorPosts
- You must be logged in to reply to this topic.