Get the name of a brand and show it in Wordpress

0

I am using the Perfect WooCommerce Brands plugin for brand management of my company's online store.

I would like to get the name of the brand and convert it into the corresponding link.

<?php $brands = wp_get_post_terms( get_the_ID(), 'pwb-brand' );
                    foreach( $brands as $brand ) { 
                            $post_excerpt = $brand->name . ' '; ?>
                            <div class="tag-brands" style="color: #fe7418 !important;font-weight: 900;font-size: 15px;">
                                    <?php echo ''.$brands;  ?>
                            </div>
            <?php } ?>

The result I get is "Array" which I understand is the complete block of brands, what I want to do is take the corresponding mark to that product and show it as a link.

    
asked by Joshua Díaz Robayna 18.10.2017 в 15:08
source

2 answers

1

Have you really tried to print the array to see what it returns ?, Why were not you going wrong:

<?php 
$brands = wp_get_post_terms(get_the_ID(), 'pwb-brand' );

foreach($brands as $brand){
    echo '<div class="tag-brands" style="color: #fe7418 !important;font-weight: 900;font-size: 15px;">' . $brand->name . '</div>';
} 
?>

The only thing I have done on your code has been to remove this portion (where you correctly collected the name but then did not print it:

$post_excerpt = $brand->name . ' ';

Then, in the echo that you did, I simply removed the S from brandS, since in the foreach the value that you must collect is the one that returns each loop (brand).

Otherwise I have cleaned the code a bit and little more ...

Greetings,

    
answered by 18.10.2017 / 15:38
source
0

In the end I solved it, like this ...

            <?php 
                $brands = wp_get_post_terms(get_the_ID(), 'pwb-brand' );
                $product_brands = wp_get_object_terms($post->ID, 'pwb-brand');

                        $slug = 'brand'; //default

                            if ( ! empty( $product_brands ) ) {
                             if ( ! is_wp_error( $product_brands ) ) {
                                     foreach( $product_brands as $brand ) {
                                    $slug = $brand->slug;
                                                                          }
                                                                     }
                                                            }


                foreach($brands as $brand){
                echo '<div class="tag-brands" style="color: #fe7418 !important;font-weight: 900;font-size: 15px;"><a href="/brands/'. $slug .'">' . $brand->name . '</a></div>';
                } 
            ?>
    
answered by 19.10.2017 в 10:34