Black Friday Sale! Get up to $30 off GP Premium! Learn more ➝

[Resolved] Using custom taxonomy description in dynamic block

Home Forums Support [Resolved] Using custom taxonomy description in dynamic block

Home Forums Support Using custom taxonomy description in dynamic block

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #1965964
    Rinke

    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

    #1965972
    David
    Staff
    Customer Support

    Hi there,

    try the GP Dynamic Content Block and set it to display the Term Description.

    #1966511
    Rinke

    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

    #1966772
    Elvin
    Staff
    Customer Support

    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).

    A wise man once said:
    "Have you cleared your cache?"

    #1969341
    Rinke

    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

    #1969385
    David
    Staff
    Customer Support

    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();
            
    } );
    #1969460
    Rinke

    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

    #1969503
    David
    Staff
    Customer Support
    #1969576
    Rinke

    Thank you so much! It works perfectly now!

    #1969584
    David
    Staff
    Customer Support
Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.