- This topic has 9 replies, 2 voices, and was last updated 4 years ago by
David.
-
AuthorPosts
-
March 15, 2022 at 10:47 pm #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).
March 16, 2022 at 4:57 am #2156495David
StaffCustomer SupportHi there,
at this time you can only filter the frontend output ( which will just be string of HTML links ) using the
generate_dynamic_element_textfilter – 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.March 17, 2022 at 4:07 am #2157684Rachel
Thank you David! I looked into the plugin code to better understand how
generate_dynamic_element_textand$term_outputare working.So if understand correctly,
$term_outputshould 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?March 17, 2022 at 8:45 am #2158101David
StaffCustomer SupportYou will need to write your own function to load the terms into an array. Probably best to use get_terms() as that has an
excludeargument.You can then
foreachyour 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 usesget_the_termsMarch 17, 2022 at 11:23 pm #2158723Rachel
Oh, ok. So
$term_outputneeds 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 useget_terms()and theforeachloop.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?
March 18, 2022 at 3:10 am #2158862David
StaffCustomer SupportThis 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 🙂
March 18, 2022 at 6:40 pm #2159641Rachel
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_texthook 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)
March 19, 2022 at 6:14 am #2159942David
StaffCustomer SupportMight 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.April 2, 2022 at 8:04 pm #2176103Rachel
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_termsbecause it is the only function that allowed me to pass in both the post ID and the arguments - got rid of the
spantags 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; });April 3, 2022 at 1:24 am #2176233David
StaffCustomer SupportAwesome – glad to hear that. And thanks for sharing your code – nice 🙂
-
AuthorPosts
- You must be logged in to reply to this topic.