Site logo

[Support request] Filtering the Dynamic List of Terms in a Content Template Block

Home Forums Support [Support request] Filtering the Dynamic List of Terms in a Content Template Block

Home Forums Support Filtering the Dynamic List of Terms in a Content Template Block

  • This topic has 9 replies, 2 voices, and was last updated 4 years ago by David.
Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #2156183
    Rachel

    I’m using a Content Template Block to format the post listing on my home page, with the post’s tags listed using Dynamic Text. Is it possible to filter/limit which tags show in this list?

    For example, could I only show tags within a specific array (by slugs or ids)? Or if I added a custom true/false field to all Tags to serve as a show/hide setting, could I use that custom field meta value to determine whether the tag shows in this list?

    One potential complicating factor – I’m also listing tags in other places, so I need to target only the Content Block without affecting other tag listings (hence why I can’t use a general WP hook to filter the tag list generally).

    #2156495
    David
    Staff
    Customer Support

    Hi there,

    at this time you can only filter the frontend output ( which will just be string of HTML links ) using the generate_dynamic_element_text filter – see here for an example:

    add_filter( 'generate_dynamic_element_text', function( $term_output, $block ){
    	if ( 'terms' === $block['attrs']['gpDynamicTextType'] ) {
    		// load up your own $term_output 
    	} 
    	return $term_output;
    },15,2);

    Theres no way to filter the get_post_terms before that.
    I would probably create a shortcode to do this as that can be used in a Headline block.

    #2157684
    Rachel

    Thank you David! I looked into the plugin code to better understand how generate_dynamic_element_text and $term_output are working.

    So if understand correctly, $term_output should be set = to a list of the terms that I want included? Do I use the term slugs, IDs, or something else? Do I write this as a string with terms separated by commas, or should it be an array?

    #2158101
    David
    Staff
    Customer Support

    You will need to write your own function to load the terms into an array. Probably best to use get_terms() as that has an exclude argument.

    You can then foreach your array to return the necessary HTML.
    Some example of that here:

    https://developer.wordpress.org/reference/functions/get_terms/#comment-1933

    Or seeing as you found the generate_dynamic_element_text in the plugin you can see how GP does it.
    The only real difference there is GP uses get_the_terms

    #2158723
    Rachel

    Oh, ok. So $term_output needs to be a string of HTML that outputs the list of tags, right? But I’m still not sure exactly how to format the HTML in my function where I use get_terms() and the foreach loop.

    I know I can look at the page source code, but I can tell there are additional variable being used in the PHP to generate it, for example the comma separator. Is there a way to expose the PHP underneath my Content Template Block element so I can copy what is being used?

    #2158862
    David
    Staff
    Customer Support

    This is the function GP runs:

    $terms = get_the_terms( $id, $block['attrs']['gpDynamicTextTaxonomy'] );
    
    if ( is_wp_error( $terms ) ) {
        return $block_content;
    }
    
    $term_items = array();
    
    foreach ( (array) $terms as $term ) {
        if ( ! isset( $term->name ) ) {
            continue;
        }
    
        if ( 'term-archives' === $link_type ) {
            $term_link = get_term_link( $term, $block['attrs']['gpDynamicTextTaxonomy'] );
    
            if ( ! is_wp_error( $term_link ) ) {
                $term_items[] = sprintf(
                    '<span class="post-term-item term-%3$s"><a href="%1$s">%2$s</a></span>',
                    esc_url( get_term_link( $term, $block['attrs']['gpDynamicTextTaxonomy'] ) ),
                    $term->name,
                    $term->slug
                );
            }
        } else {
            $term_items[] = sprintf(
                '<span class="post-term-item term-%2$s">%1$s</span>',
                $term->name,
                $term->slug
            );
        }
    }

    Does that help ? Let us know – if it doesn’t we’ll see if we can put something together 🙂

    #2159641
    Rachel

    I’m sorry, I’m pretty new to programming, and honestly I know more JavaScript than PHP. So I understand how the filter hooks should work in theory, but I’m having trouble applying it here.

    Based on the first snippet you gave it seemed like if I used the generate_dynamic_element_text hook then I would be stepping in at the point in the code after the terms had been grabbed, but before they are turned into HTML. So I could just filter which terms are included in the output.

    But then you said that I have to actually get the terms and return HTML in my custom function, so I figured – ok, then I need to know how the HTML is currently being generated, so I can basically just copy the code that is happening between getting the terms and returning HTML, then add in my filter code.

    And now this second code snippet has me even more confused…so maybe linking the page I’m working on will help – I’m trying to filter the “skills” list on each “project” post block. (its a work in progress, so link is in private area)

    #2159942
    David
    Staff
    Customer Support

    Might be easier to Remove the Dynamic data and instead create a custom shortcode with this snippet:

    add_shortcode('cat_listing', function($html){
        $html = '';
        $args = array(  
            'hide_empty' => 1,    
            'exclude' => array(1,2,3) // Exclude IDS
        ); 
        $categories = get_categories($args);
        foreach( $categories as $cat ) {
            $html .= '<span class="post-term-item term-'.$cat->cat_name.'">'.$cat->cat_name.'</span>';
        }
        return $html;
    });

    Then you can add the [cat_listing] shortcode in the Headline Block.

    #2176103
    Rachel

    Apologies for my delayed response.

    Thank you so much David! Swapping the dynamic text for a shortcode was the perfect trick! I need to learn more about shortcodes, they’ve been the lightbulb solution for me before.

    I made a few adjustments to the code you provided:

    • used ACF to add a field to use as the filter
    • used wp_get_post_terms because it is the only function that allowed me to pass in both the post ID and the arguments
    • got rid of the span tags in the output (because they were causing unwanted line break and text overflow, and I’m not sure if they’re necessary?)
    • added comma separators (with a conditional statement that uses the array index to exclude the ending comma)
    add_shortcode('skill_list', function($html){
        $html = '';
        $post_id = get_the_ID();
        $args = array(  
    	'meta_key' => 'major',
    	'meta_value' => true
        ); 
        $skills = wp_get_post_terms($post_id, 'post_tag', $args);
        foreach( $skills as $i => $s ) {
            $html .= ' '.$s->name;
    	if ($i !== array_key_last($skills))
    	    $html .= ',';
        }
        return $html;
    });
    #2176233
    David
    Staff
    Customer Support

    Awesome – glad to hear that. And thanks for sharing your code – nice 🙂

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