0

I am using Wordpress and I have set up a shop in woocommerce. I am trying to display brand name as link on product page and I am having difficulties with the code. So far, I have:

 <?php $brands = wp_get_post_terms( $post->ID, 'product_brand', array("fields" => "all") );

echo "Brend: ";
  foreach( $brands as $brand ) {
       $url = get_term_link( $brand->slug, 'product_brand' );
       echo '<a href="' . $url . $brand->name '"></a>';

}?>

I get the following error:

Parse error: syntax error, unexpected ''">'' (T_CONSTANT_ENCAPSED_STRING), expecting ',' or ';' in your code on line 6

I am not able to see what am i doing wrong. Any help would be highly appreciated.

Nancy
  • 504
  • 2
  • 6
  • 21

2 Answers2

2

You are missing the concatenation here $brand->name:

echo '<a href="' . $url . $brand->name '"></a>';

This should be:

echo '<a href="' . $url . $brand->name. '"></a>';

Update:

Also note that, you need to use $brand-name inside the <a></a>.

echo '<a href="' . $url . $brand->name .'">'.$brand->name.'</a>';

Update 2:

Also note that, you don't need to concat $brand->name with $url, its already having your brand name.

echo '<a href="' . $url .'">'.$brand->name.'</a>';
devpro
  • 16,184
  • 3
  • 27
  • 38
  • Thanx for the reply, I tried this and the error is gone. But I can not see any brand name now. Any suggestions? Thanx. – Nancy Sep 29 '16 at 09:08
  • @Nancy: `print_r($brands)` use this before your loop and share the result – devpro Sep 29 '16 at 09:09
  • Ok, did that and the result is Brend: Array ( [0] => WP_Term Object ( [term_id] => 65 [name] => Nokia [slug] => nokia [term_group] => 0 [term_taxonomy_id] => 65 [taxonomy] => product_brand [description] => [parent] => 0 [count] => 1 [filter] => raw ) [1] => WP_Term Object ( [term_id] => 66 [name] => Samsung [slug] => samsung [term_group] => 0 [term_taxonomy_id] => 66 [taxonomy] => product_brand [description] => [parent] => 0 [count] => 1 [filter] => raw ) ) – Nancy Sep 29 '16 at 09:11
  • check update @Nancy – devpro Sep 29 '16 at 09:13
  • Oh that's right. Thank you! – Nancy Sep 29 '16 at 09:15
  • @Nancy: if its work for u, dont forget to accept the answer this will help to others. – devpro Sep 29 '16 at 09:15
  • I just did. But just another small question - now I see brand name twice in my link like /samsungSamsung instead of /Samsung and when I click no products for that brand shows *because samsungSamsung doesnt exist, but Samsung does). How can I repair this? – Nancy Sep 29 '16 at 09:26
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/124511/discussion-between-devpro-and-nancy). – devpro Sep 29 '16 at 09:27
1

check :

<?php $brands = wp_get_post_terms( $post->ID, 'product_brand', array("fields" => "all") );

echo "Brend: ";
  foreach( $brands as $brand ) {
       $url = get_term_link( $brand->slug, 'product_brand' );
       echo '<a href="' . $url . $brand->name .'">'.$brand->name.'</a>';

}?>
Nitya Kumar
  • 967
  • 8
  • 14