Site logo

Archived topic

Hide parent taxonomy in List of terms of Dynamic text type

1 reply · Started by Takeru on April 5, 2022

Viewing posts 1–2 of 2

I want to hide the parent taxonomy in the List of terms of the Dynamic text type.

Is there a way to do this?

Hi Marumoro,

To achieve such, you can try adding this PHP snippet:

add_filter( 'generate_dynamic_element_text_output', function( $term_output, $block ){
	if ( 'terms' === $block['attrs']['gpDynamicTextType']  && 'd68a6ef2' === $block['attrs']['uniqueId']) {
		
		$terms = get_the_terms( get_the_id(), $block['attrs']['gpDynamicTextTaxonomy'] );

						if ( is_wp_error( $terms ) ) {
							return $block_content;
						}

						$term_items = array();

						foreach ( (array) $terms as $term ) {
							var_dump(substr_count(get_category_parents($term->term_id),"/"));
							if(substr_count(get_category_parents($term->term_id),"/")>=2) {
							if ( ! isset( $term->name ) ) {
								continue;
							}

							
								$term_items[] = sprintf(
									'<span class="post-term-item term-%2$s">%1$s</span>',
									$term->name,
									$term->slug
								);
							}
						}

						if ( empty( $term_items ) ) {
							return '';
						}

						$sep = isset( $block['attrs']['gpDynamicTextTaxonomySeparator'] ) ? $block['attrs']['gpDynamicTextTaxonomySeparator'] : ', ';
						$term_output = implode( $sep, $term_items );

						if ( ! empty( $block['attrs']['gpDynamicTextBefore'] ) ) {
							$term_output = $block['attrs']['gpDynamicTextBefore'] . $term_output;
						}
	} 
	return $term_output;
},15,2);

Kindly replace d68a6ef2 with the unique ID of your Dynamic Headline Block.

How to get the unique ID: https://share.getcloudapp.com/z8uLkDEz

Hope this helps.

This archived topic is closed to new replies.