Skip Navigation

[Closed] Taxonomy shortcode not working inside content template.

This support ticket is created 3 years, 4 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

This topic contains 1 reply, has 2 voices.

Last updated by Nigel 3 years, 4 months ago.

Author
Posts
#2135537
Screenshot (238).png
Screenshot (237).png

We found this toolset shortcode in the docs
(https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#wpv-taxonomy-title).Now we are using the shortcode for displaying a custom taxonomy field inside a template, so intead it's just displaying the shortcode as it is. What should we do?

#2135679

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

The context for the shortcodes that output term meta (the custom fields for your taxonomy) must be a taxonomy term, i.e. when you are viewing the archive for a taxonomy term, or if you created a View (with the legacy editor) to query taxonomy terms.

In your Content Template the context is a post, and while you can output the terms assigned to the post, you can't output meta data of those terms.

One solution would be to create a taxonomy View to provide the correct context, but the simplest solution is probably to register a custom shortcode to return the term_id of the term assigned to the post, and use that in the wpv-taxonomy-field shortcode to force which term you are retrieving the meta data for.

So, register a custom shortcode with the following code (you can add a Code Snippet at Toolset > Settings > Custom Code):

add_shortcode('term-id', function ($atts = [] ) {

    $output = "";

    if ( isset( $atts['tax'] ) ){

        global $post;
        $terms = get_the_terms( $post->ID, $atts['tax'] );

        if ( is_array( $terms ) ){
            $output = $terms[0]->term_id;
        }
    }

    return $output;
});

Then go to Toolset > Settings > Front-end Content, and add this term-id shortcode to the Third-party shortcode arguments setting.

Now go back to your template, and you should be able to use the following shortcode:

[wpv-taxonomy-field term_id='[term-id tax="colour"]' name='wpcf-logo']

(You need to use the wpcf- prefix for your taxonomy custom field.)

I don't know what your taxonomy slug is. In my example above it is "colour"; you'll need to change that to your taxonomy.

The topic ‘[Closed] Taxonomy shortcode not working inside content template.’ is closed to new replies.