0
votes

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: https://developer.wordpress.org/reference/functions/get_the_terms/

I added this code block to functions.php:

/**
 * 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->post_type;

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

    $out = array();

    foreach ( $taxonomies as $taxonomy_slug => $taxonomy ){

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

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

And then in a Divi code block put this:

<?php echo wpdocs_custom_taxonomies_terms_links(); ?>

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

1

1 Answers

0
votes

The code works fine in Divi... turn the code snipped into a shortcode to put it on a divi page.

You can use Insert PHP Code Snippet to create the shortcode.