2016-09-29 93 views
0

我使用Wordpress,並在woocommerce中設置了一個商店。我試圖在產品頁面上顯示品牌名稱作爲鏈接,並且我在代碼方面遇到困難。到目前爲止,我有:顯示變量作爲鏈接

<?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>'; 

}?> 

我得到以下錯誤:

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

我沒能看到我在做什麼錯。任何幫助將不勝感激。

+0

回聲'; –

+0

'echo'';'用這個 – devpro

回答

2

您在這裏$brand->name缺少串聯:

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

這應該是:

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

更新:

還要注意的是,你需要使用$名牌裏面的<a></a>

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

更新2:

還要注意的是,你不需要Concat的$brand->name$url,它已經有你的品牌名稱。

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

Thanx來回復,我試過了,錯誤消失了。但我現在看不到任何品牌名稱。有什麼建議麼?感謝名單。 – Nancy

+0

@Nancy:'print_r($ brands)'在你的循環之前使用它並且共享結果 – devpro

+0

好吧,是這樣做的,結果是 Brend:Array([0] => WP_Term Object([term_id] => 65 [名稱] =>諾基亞[slug] =>諾基亞[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

1

檢查:

<?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>'; 

}?> 
+0

請在'' – devpro

+0

裏面添加$ brand-> name好的,謝謝@devpro。現在它的罰款:) –

+0

是的它現在罰款:)我的投票你 – devpro