- This topic has 9 replies, 3 voices, and was last updated 2 years, 6 months ago by David.
-
AuthorPosts
-
October 17, 2021 at 5:18 am #1965964Rinke
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,
RinkeOctober 17, 2021 at 5:31 am #1965972DavidStaffCustomer SupportHi there,
try the GP Dynamic Content Block and set it to display the Term Description.
October 17, 2021 at 12:48 pm #1966511RinkeHi 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,
RinkeOctober 17, 2021 at 10:04 pm #1966772ElvinStaffCustomer SupportHi 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).
October 20, 2021 at 2:11 am #1969341RinkeHi 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,
RinkeOctober 20, 2021 at 3:01 am #1969385DavidStaffCustomer SupportTry 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(); } );
October 20, 2021 at 4:43 am #1969460RinkeHi 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,
RinkeOctober 20, 2021 at 5:52 am #1969503DavidStaffCustomer SupportMade a tweak to the code above:
https://generatepress.com/forums/topic/using-custom-taxonomy-description-in-dynamic-block/#post-1969385October 20, 2021 at 7:22 am #1969576RinkeThank you so much! It works perfectly now!
October 20, 2021 at 7:29 am #1969584DavidStaffCustomer SupportGlad to hear that!
-
AuthorPosts
- You must be logged in to reply to this topic.