0

I need to add some shotrcodes for many taxonomies and I would like to display the value terms on a product page of woocommerce. I tried with this but doesn't work, in this case the taxonomy name is "pagamento"

$terms = get_terms( 'pagamento' );
 if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
     echo '<ul>';
     foreach ($terms as $term) {
     // $taxonomy name pagamento
     $pagamento = get_field('pagamento', $term->taxonomy.'_'.$term->term_id);
}
     echo '</ul>';
 }

add_shortcode( 'pagamento', 'na_display_pagamento' );

2 Answers 2

0

I fix it with:

function the_function_name() { echo get_the_term_list( $product->ID, 'YOURTAXONOMY', 'Label Taxonomy: ', ', ', ''); } add_shortcode('mycustomshortcode', 'the_function_name');

0

Please try with this:

$terms = get_terms( array( 'taxonomy' => 'pagamento' ) );

Since 4.5.0, taxonomies should be passed via the ‘taxonomy’ argument in the $args, see: https://developer.wordpress.org/reference/functions/get_terms/#description

UPDATE:

Your shortcode should looks like this:

function na_display_pagamento() {
    $output = "";
    $terms = get_terms( 'pagamento' );
    if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
        $output .= '<ul>';
        foreach ($terms as $term) {
            // $taxonomy name pagamento
            $pagamento = get_field( 'pagamento', $term->taxonomy.'_'.$term->term_id );
            if ( $pagamento ) {
                $output .= '<li>';
                    $output .= $pagamento;
                $output .= '</li>';
            }
        }
        $output .= '</ul>';
    }
    return $output;
}
add_shortcode( 'pagamento', 'na_display_pagamento' );

Instead of echo the value, you can store the value in variable then return it.

Please see: https://codex.wordpress.org/Shortcode_API

3
  • Hi, thank you very much for your reply. I tried changing the line but nothing, I have not the value term in my shortcode :( Oct 20, 2022 at 9:29
  • @MarcoFabris I just updated my answer. Oct 21, 2022 at 5:40
  • Thank you for your time @Tommy Oct 22, 2022 at 7:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.