How to display custom taxonomy on a WooCommerce product using Divi theme

admin

Administrator
Staff member
I am using Divi theme on Wordpress with WooCommerce and I have created a custom taxonomy called Brand using a plugin.
Now I need to display the Brand field on the product page. I don't know php but I can add code to the theme files if I know where to put it.
I have seen a lot of code for similar scenarios but most of them are talking about registering the taxonomy also, where I have already done that I believe (using CPTUI) so I have taken the code for this from the comments on this page: <a href="https://developer.wordpress.org/reference/functions/get_the_terms/" rel="nofollow noreferrer">https://developer.wordpress.org/reference/functions/get_the_terms/</a>

I added this code block to functions.php:

Code:
/**
 * Get taxonomies terms links.
 *
 * @see get_object_taxonomies()
 */
function wpdocs_custom_taxonomies_terms_links() {
    // Get post by post ID.
    if ( ! $post = get_post() ) {
        return '';
    }

    // Get post type by post.
    $post_type = $post-&gt;post_type;

    // Get post type taxonomies.
    $taxonomies = get_object_taxonomies( $post_type, 'objects' );

    $out = array();

    foreach ( $taxonomies as $taxonomy_slug =&gt; $taxonomy ){

        // Get the terms related to post.
        $terms = get_the_terms( $post-&gt;ID, $taxonomy_slug );

        if ( ! empty( $terms ) ) {
            $out[] = "&lt;h2&gt;" . $taxonomy-&gt;label . "&lt;/h2&gt;\n&lt;ul&gt;";
            foreach ( $terms as $term ) {
                $out[] = sprintf( '&lt;li&gt;&lt;a href="%1$s"&gt;%2$s&lt;/a&gt;&lt;/li&gt;',
                    esc_url( get_term_link( $term-&gt;slug, $taxonomy_slug ) ),
                    esc_html( $term-&gt;name )
                );
            }
            $out[] = "\n&lt;/ul&gt;\n";
        }
    }
    return implode( '', $out );
}
?&gt;

And then in a Divi code block put this:

Code:
&lt;?php echo wpdocs_custom_taxonomies_terms_links(); ?&gt;

But nothing happened. No errors in console, just nothing. The taxonomy has been populated for this product. Please help!