Site logo

[Support request] Display Custom Taxonomy Name and Description

Home Forums Support [Support request] Display Custom Taxonomy Name and Description

Home Forums Support Display Custom Taxonomy Name and Description

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #2582109
    Pete Williams

    I’m using Generate Press Pro, Generate Blocks (free), ACF, and CPT UI.

    I’ve used CPT UI to create a custom taxonomy (tool_categories) and then added a handful of items within this taxonomy (e.g. ToolCat1, ToolCat2, ToolCat3, etc.).

    I’d like to use a Query Loop and Dynamic Data to show the Name and Description of these items.

    Using a Query Loop and Dynamic Data I’ve already got the products (posts) that are linked to these items to show, but I’m struggling to do the same with the Custom Taxonomy Name and Description.

    I’ve tried various code that I’ve found within the forum, but I’m unable to get it working.

    The ideal results would be to display:

    ToolCat1 Name
    ToolCat1 Description
    List of ToolCat1 Posts (this is already working)

    ToolCat2 Name
    ToolCat2 Description
    List of ToolCat2 Posts (this is already working)

    ToolCat3 Name
    ToolCat3 Description
    List of ToolCat3 Posts (this is already working)

    etc. etc.

    This way, when I add a new post and link it to one of the above categories, it will be added to the page automatically. i.e. I’d not have to edit the post each time I add a new product.

    This is the post that I thought would help best but when I use it, nothing happens so I’m clearly doing something wrong:

    https://generatepress.com/forums/topic/custom-taxonomy-not-displaying-in-dynamic-data/

    Are you able to help, please?

    #2582495
    David
    Staff
    Customer Support

    Hi there,

    the GB Query Loop only handles post types.
    For that you would need to use static content or a shortcode.
    For example add the ToolCat3 as static content and use a Shortcode to display its description.

    1. Create your shortcode with a PHP Snippet:

    function custom_taxonomy_description_shortcode( $atts ) {
        // Extract the attributes from the shortcode
        extract( shortcode_atts( array(
            'term' => ''
        ), $atts ) );
    
        // Check if the term attribute is set
        if ( empty( $term ) ) {
            return '';
        }
    
        // Get the term object
        $term_obj = get_term_by( 'name', $term, 'your_taxonomy_name' );
    
        // Check if the term object is valid
        if ( ! $term_obj || is_wp_error( $term_obj ) ) {
            return '';
        }
    
        // Get the term description
        $description = $term_obj->description;
    
        // Return the term description
        return $description;
    }
    add_shortcode( 'tax_description', 'custom_taxonomy_description_shortcode' );
    

    In the code, change: your_taxonomy_name to the name of custom taxonomy.

    2. Add the [tax_description term="your_term_name"] shortcode to a headline block.

    #2582561
    Pete Williams

    Sorry, David.

    How do I add a shortcode to the headline block? I thought shortcodes required a shortcode block?

    Also, in the shortcode, term="your_term_name" using my previous example would just be term="ToolCat3", right?

    Thanks for your help!

    #2582898
    David
    Staff
    Customer Support

    GB Headlines support shortcodes, if you add it in place of ( or within ) the static text then it will render.

    Correct if the term is ToolCat3 then the shortcode would be:

    [tax_description term="ToolCat3"]

    #2583050
    Pete Williams

    Thanks, David. I didn’t realise they could render shortcodes.

    I’ve got this working now. Not the optimal solution but a workaround that does the job.

    Do you know, by any chance, if there are any plans to add support for custom taxonomies within the Query Loop?

    Thanks again for your help.

    #2583780
    David
    Staff
    Customer Support

    The Query Loop already supports custom taxonomies… but what it can’t do is create content outside of its loop, such as a header for the title and the description.

    If you we’re customizing a theme archive template then you could use a block element to add the title and description before the loop. And this could be automatic as it would use the current object term to get that data.

    But if you’re creating static pages or multiloop templates then the current object is not related to the query loop, so the data sources have to be explicitly defined. Which is what we need the shortcode for today.

    In the future GB Pro will support more complex data types and sources that will do away with the need for that kind of shortcode. But the reality is, i think there will always be a use case for shortcodes as there are so many options users need now.

    I hope that helps – if of course i have misinterpreted the need then let me know.

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