Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Dynamic term list doesn’t add class to each span (GB 2.0)

  • Hi GB team!
    I’m trying to add a custom icon for each term in a dynamic term list.
    With the previous version of GB I was able to do so manually with css by targeting the specific class that the block added to each span. Here’s an example:

    <p class="gb-headline gb-headline-355c1760 gb-headline-text dynamic-term-class">
    <span class="post-term-item term-termico">Termico</span>
    <span class="post-term-item term-durevole">Durevole</span>
    <span class="post-term-item term-easy-care">Easy care</span>
    <span class="post-term-item term-lana">Lana</span>
    <span class="post-term-item term-morbido-e-avvolgente">Morbido e avvolgente</span>
    <span class="post-term-item term-traspirante">Traspirante</span>
    </p>

    With the new version of GB though it seems that the “post-term-item” class and the specific class “term-XXX” are gone. Like this:

    <p class="gb-text senza-pag-prodotto gb-text-868520f6">
    <span>Senza Glutine</span>
    <span>Senza latte</span>
    <span>Senza nocciole</span>
    <span>Senza zuccheri aggiunti</span>
    <span>Vegan Friendly</span>
    </p>

    Is there a way to add them OR an easier way to add a custom (svg) icon for each term?

    Thanks!
    Lorenzo

  • Hi there,

    Try this PHP snippet, but you will need to assign the term link to the terms, so each term is an <a>, as the <span> doesn’t work with this filter.

    add_filter( 'term_links-category', 'add_category_class_to_term_links', 10, 1 );
    
    function add_category_class_to_term_links( $list_items ) {
        foreach ( $list_items as &$item ) {
            // Extract the term name from the <a> tag
            if ( preg_match( '/>([^<]+)</', $item, $matches ) ) {
                $term_name = sanitize_html_class( $matches[1] ); // Sanitize term name
                $class = 'post-term-item term-' . strtolower( $term_name ); // Force lowercase
    
                // Check if the class attribute exists
                if ( strpos( $item, 'class="' ) === false ) {
                    // No class exists, add it
                    $item = str_replace( '<a ', '<a class="' . esc_attr( $class ) . '" ', $item );
                } else {
                    // Class exists, check if our class is already present
                    if ( strpos( $item, $class ) === false ) {
                        // Append the class if it’s not already there
                        $item = preg_replace( '/class="([^"]*)"/', 'class="$1 ' . esc_attr( $class ) . '"', $item );
                    }
                    // If the class is already present, do nothing
                }
            }
        }
        unset( $item ); // Unset the reference after the loop
    
        return $list_items;
    }

    Adding PHP: https://docs.generatepress.com/article/adding-php/

  • Hi Ying,
    thank you for your reply.
    Unfortunately it seems not to be working.
    I converted the <span> in <a> as you suggested, but the result is as follows:

    <p class="gb-text senza-pag-prodotto gb-text-868520f6">
    <a href="https://dev.laperladitorino.it/allergeni/senza-nocciole/" rel="tag">Senza nocciole</a>
    <a href="https://dev.laperladitorino.it/allergeni/vegan-friendly/" rel="tag">Vegan friendly</a>
    </p>

    Lorenzo

  • Hi there,

    Try this snippet:

    add_filter('generateblocks_dynamic_tag_output', function ($output, $options) {
    	if (isset($options['term_class'])) {
    		if (strpos($output, '<span>') !== false) {
    			// Use a regular expression to find all spans and their content
    			$pattern = '/<span>(.*?)<\/span>/';
    			preg_match_all($pattern, $output, $matches);
    
    			if (!empty($matches[0]) && !empty($matches[1])) {
    				// Loop through each match and replace with span + classes
    				foreach ($matches[0] as $key => $original_span) {
    					$term_text = $matches[1][$key];
    					$term_slug = sanitize_title($term_text); // Convert to slug format
    
    					// Create the new span with both classes
    					$new_span = '<span class="post-term-item term-' . $term_slug . '">' . $term_text . '</span>';
    
    					// Replace in the output
    					$output = str_replace($original_span, $new_span, $output);
    				}
    			}
    		}
    	}
    
    	return $output;
    }, 10, 2);

    Then, add the term_class option to the Term List dynamic tag so it looks like this:
    {{term_list tax:category|term_class:true}}

    This works only with the term list without a link.

  • Hi Alvind,
    works like a charm!

    Are you anyway going to embed the class in the new GB as the old one in a next update?
    It was really useful!

    I’ll open a new thread for an easy way to add an icon to a term without CSS 🙂

    Thanks!
    Lorenzo

  • Are you anyway going to embed the class in the new GB as the old one in a next update?

    There are no plans for this at the moment, but we’ll see what improvements we can make in that area.

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.