Archived topic
Using custom taxonomy description in dynamic block
9 replies · Started by Rinke on October 17, 2021
Hi there,
I'm using a custom post type, with custom taxonomy. I'm also using elements to make a content template. Now I want to add a dynamic block containing the description of one of the taxonomies (let's say: brand). I've read through the forum and through your documentation, and while I feel like I'm close, I can't get it to work.
I tried various things, including choosing a headline-block, selecting dynamic options -> dynamic text data: term meta, and adding 'brand' to the meta field name. It doesn't show up though. Any ideas on how to accomplish this?
Cheers,
Rinke
Hi there,
try the GP Dynamic Content Block and set it to display the Term Description.
Hi David,
Thanks for your quick reply. But that won't let me choose which taxonomy to use.
Also, I now realize my explanation wasn't complete. I have a taxonomy 'brand', with, let's say, entries 'company A' to 'company E'. These entries have a description associated with each of them. I would like to add a dynamic block to the content template that shows the description of these companies. Thus, when a post is linked to Brand>Company A, I would like to show the description associated with that. Is that possible?
Cheers,
Rinke
Hi Rinke,
If you need to pick a specific taxonomy, you have to create a helper shortcode for this instead of the dynamic block.
Here's a PHP snippet for the shortcode:
add_shortcode( 'display_term_desc', function($atts) {
ob_start();
$atts = shortcode_atts( array(
'term_slug' => '',
'taxonomy' => '',
), $atts, 'dynamic_wpsp' );
$term = get_term_by( 'slug', $atts['term_slug'], $atts['taxonomy'] );
$desc = $term->description;
echo $desc;
return ob_get_clean();
} );
Example usage:
[display_term_desc term_slug="uncategorized" taxonomy="category"]
Where term-slug is the slug of the term and the taxonomy is the taxonomy of the term. This only displays the term description as a string of text. (no html element wrapper).
Hi Elvin,
Thanks for your reply, that worked! However, I now have to specify the term. Is it possible to make it dynamic so it gets the description of the term which is associated with a post? That way I can use it in a content template in elements.
Cheers,
Rinke
Try this instead:
add_shortcode( 'display_term_desc', function() {
$terms = get_the_terms( get_the_ID(), 'category' );
if ( empty($terms) ) {
return;
}
ob_start();
echo term_description( $terms[0]->term_id, 'category' );
return ob_get_clean();
} );
Hi David,
Thanks for your quick reply! However, it does not seem to work. I tried replacing 'category' with the name of the taxonomy. I'm using custom post types and a custom taxonomy. Might that have anything to do with it?
Cheers,
Rinke
Made a tweak to the code above:
https://generatepress.com/forums/topic/using-custom-taxonomy-description-in-dynamic-block/#post-1969385
Thank you so much! It works perfectly now!
Glad to hear that!