Php Wordpress Woocommerce在商店页面上显示属性
我想给wordpress的商店页面添加一些属性。 我在Stackoverflow上找到的这段代码显示了所有属性标签,但所有属性名称都相同Php Wordpress Woocommerce在商店页面上显示属性,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我想给wordpress的商店页面添加一些属性。 我在Stackoverflow上找到的这段代码显示了所有属性标签,但所有属性名称都相同 add_action('woocommerce_after_shop_loop_item_title','add_attribute'); function add_attribute() { global $product; $product_attributes = array( 'pa_country','pa_class','pa_faction',
add_action('woocommerce_after_shop_loop_item_title','add_attribute');
function add_attribute() {
global $product;
$product_attributes = array( 'pa_country','pa_class','pa_faction','pa_gender' );
$attr_output = array();
foreach( $product_attributes as $taxonomy ){
if( taxonomy_exists($taxonomy) ){
$label_name = get_taxonomy( $taxonomy )->labels->singular_name;
$value = $product->get_attribute('pa_country','pa_class','pa_faction','pa_gender');
if( ! empty($value) ){
$attr_output[] = '<span class="'.$taxonomy.'">'.$label_name.': '.$value.'</span>';
}
}}
echo '<div class="product-attributes">'.implode( '<br>', $attr_output ).'</div>';
add_action('woocommerce_在'u shop_loop_item_title'之后,'add_属性');
函数add_属性(){
全球$产品;
$product_attributes=数组('pa_country'、'pa_class'、'pa_parties'、'pa_gender');
$attr_output=array();
foreach($product_属性作为$TAXINORYOM){
如果(分类法_存在($taxonomy)){
$label\u name=get\u taxonomy($taxonomy)->labels->singular\u name;
$value=$product->get_属性('pa_country'、'pa_class'、'pa_party'、'pa_gender');
如果(!空($value)){
$attr_output[]='.$label_name.':'.$value.';
}
}}
回显“”。内爆(“
”,$attr\u输出)。“”;
}
我只需要一点帮助,让它显示所有正确的属性。我解决了这个问题。它不漂亮,但很管用
add_action( 'woocommerce_after_shop_loop_item_title', 'display_size_attribute', 5 );
function display_size_attribute() {
global $product;
if ( $product->is_type('simple') ) {
$taxonomy = 'pa_country';
echo '<span class="attribute-s">Country: ' . $product->get_attribute($taxonomy) . '</span>','<br>';
}
if ( $product->is_type('simple') ) {
$taxonomy = 'pa_class';
echo '<span class="attribute-s">Class: ' . $product->get_attribute($taxonomy) . '</span>','<br>';
}
if ( $product->is_type('simple') ) {
$taxonomy = 'pa_faction';
echo '<span class="attribute-s">Faction: ' . $product->get_attribute($taxonomy) . '</span>','<br>';
}
if ( $product->is_type('simple') ) {
$taxonomy = 'pa_gender';
echo '<span class="attribute-s">Gender: ' . $product->get_attribute($taxonomy) . '</span>','<br>';
}}
add_action('woocommerce_在_-shop_-loop_-item_-title'之后,'display_-size_-attribute',5);
函数显示\大小\属性(){
全球$产品;
如果($product->is_type('simple')){
$taxonomy='pa_country';
echo“Country:”.$product->get_属性($taxonomy)。“
”;
}
如果($product->is_type('simple')){
$taxonomy='pa_class';
echo“Class:”.$product->get_属性($taxonomy)。“
”;
}
如果($product->is_type('simple')){
$taxonomy='pa_派系';
echo“派系:”.$product->get_属性($taxonomy)。“
”;
}
如果($product->is_type('simple')){
$taxonomy='pa_性别';
回显“性别:”。$product->get_属性($TAXINORYOM)。“
”;
}}
我解决了这个问题。它不漂亮,但很管用
add_action( 'woocommerce_after_shop_loop_item_title', 'display_size_attribute', 5 );
function display_size_attribute() {
global $product;
if ( $product->is_type('simple') ) {
$taxonomy = 'pa_country';
echo '<span class="attribute-s">Country: ' . $product->get_attribute($taxonomy) . '</span>','<br>';
}
if ( $product->is_type('simple') ) {
$taxonomy = 'pa_class';
echo '<span class="attribute-s">Class: ' . $product->get_attribute($taxonomy) . '</span>','<br>';
}
if ( $product->is_type('simple') ) {
$taxonomy = 'pa_faction';
echo '<span class="attribute-s">Faction: ' . $product->get_attribute($taxonomy) . '</span>','<br>';
}
if ( $product->is_type('simple') ) {
$taxonomy = 'pa_gender';
echo '<span class="attribute-s">Gender: ' . $product->get_attribute($taxonomy) . '</span>','<br>';
}}
add_action('woocommerce_在_-shop_-loop_-item_-title'之后,'display_-size_-attribute',5);
函数显示\大小\属性(){
全球$产品;
如果($product->is_type('simple')){
$taxonomy='pa_country';
echo“Country:”.$product->get_属性($taxonomy)。“
”;
}
如果($product->is_type('simple')){
$taxonomy='pa_class';
echo“Class:”.$product->get_属性($taxonomy)。“
”;
}
如果($product->is_type('simple')){
$taxonomy='pa_派系';
echo“派系:”.$product->get_属性($taxonomy)。“
”;
}
如果($product->is_type('simple')){
$taxonomy='pa_性别';
回显“性别:”。$product->get_属性($TAXINORYOM)。“
”;
}}
您的代码中有一些小错误。请尝试以下操作:
add_action('woocommerce_after_shop_loop_item_title', 'display_shop_loop_product_attributes');
function display_shop_loop_product_attributes() {
global $product;
// Define you product attribute taxonomies in the array
$product_attribute_taxonomies = array( 'pa_country', 'pa_class', 'pa_faction', 'pa_gender' );
$attr_output = array(); // Initializing
// Loop through your defined product attribute taxonomies
foreach( $product_attribute_taxonomies as $taxonomy ){
if( taxonomy_exists($taxonomy) ){
$label_name = wc_attribute_label( $taxonomy, $product );
$term_names = $product->get_attribute( $taxonomy );
if( ! empty($term_names) ){
$attr_output[] = '<span class="'.$taxonomy.'">'.$label_name.': '.$term_names.'</span>';
}
}
}
// Output
echo '<div class="product-attributes">'.implode( '<br>', $attr_output ).'</div>';
}
add_action('woocommerce_after_shop_loop_item_title', 'display_shop_loop_product_attributes');
function display_shop_loop_product_attributes() {
global $product;
// Only for simple products
if ( ! $product->is_type( 'simple' ) ) return;
// Define you product attribute taxonomies in the array
$product_attribute_taxonomies = array( 'pa_country', 'pa_class', 'pa_faction', 'pa_gender' );
$attr_output = array(); // Initializing
// Loop through your defined product attribute taxonomies
foreach( $product_attribute_taxonomies as $taxonomy ){
if( taxonomy_exists($taxonomy) ){
$label_name = wc_attribute_label( $taxonomy, $product );
$term_names = $product->get_attribute( $taxonomy );
if( ! empty($term_names) ){
$attr_output[] = '<span class="'.$taxonomy.'">'.$label_name.': '.$term_names.'</span>';
}
}
}
// Output
echo '<div class="product-attributes">'.implode( '<br>', $attr_output ).'</div>';
}
add_action('woocommerce_在_shop_loop_item_title'之后,'display_shop_loop_product_attributes');
功能显示\车间\循环\产品\属性(){
全球$产品;
//在数组中定义产品属性分类
$product_attribute_taxonomies=数组('pa_country'、'pa_class'、'pa_party'、'pa_gender');
$attr_output=array();//正在初始化
//循环浏览已定义的产品属性分类
foreach($product\U属性\U分类法为$TAXINOLY){
如果(分类法_存在($taxonomy)){
$label\u name=wc\u attribute\u label($taxonomy,$product);
$term\u names=$product->get\u属性($taxonomy);
如果(!空($term_names)){
$attr_output[]='.$label_name.:'.$term_name.';
}
}
}
//输出
回显“”。内爆(“
”,$attr\u输出)。“”;
}
代码进入活动子主题(或活动主题)的functions.php文件。测试和工作
仅对于简单产品,您将使用以下选项:
add_action('woocommerce_after_shop_loop_item_title', 'display_shop_loop_product_attributes');
function display_shop_loop_product_attributes() {
global $product;
// Define you product attribute taxonomies in the array
$product_attribute_taxonomies = array( 'pa_country', 'pa_class', 'pa_faction', 'pa_gender' );
$attr_output = array(); // Initializing
// Loop through your defined product attribute taxonomies
foreach( $product_attribute_taxonomies as $taxonomy ){
if( taxonomy_exists($taxonomy) ){
$label_name = wc_attribute_label( $taxonomy, $product );
$term_names = $product->get_attribute( $taxonomy );
if( ! empty($term_names) ){
$attr_output[] = '<span class="'.$taxonomy.'">'.$label_name.': '.$term_names.'</span>';
}
}
}
// Output
echo '<div class="product-attributes">'.implode( '<br>', $attr_output ).'</div>';
}
add_action('woocommerce_after_shop_loop_item_title', 'display_shop_loop_product_attributes');
function display_shop_loop_product_attributes() {
global $product;
// Only for simple products
if ( ! $product->is_type( 'simple' ) ) return;
// Define you product attribute taxonomies in the array
$product_attribute_taxonomies = array( 'pa_country', 'pa_class', 'pa_faction', 'pa_gender' );
$attr_output = array(); // Initializing
// Loop through your defined product attribute taxonomies
foreach( $product_attribute_taxonomies as $taxonomy ){
if( taxonomy_exists($taxonomy) ){
$label_name = wc_attribute_label( $taxonomy, $product );
$term_names = $product->get_attribute( $taxonomy );
if( ! empty($term_names) ){
$attr_output[] = '<span class="'.$taxonomy.'">'.$label_name.': '.$term_names.'</span>';
}
}
}
// Output
echo '<div class="product-attributes">'.implode( '<br>', $attr_output ).'</div>';
}
add_action('woocommerce_在_shop_loop_item_title'之后,'display_shop_loop_product_attributes');
功能显示\车间\循环\产品\属性(){
全球$产品;
//仅适用于简单产品
如果(!$product->是_类型('simple'))返回;
//在数组中定义产品属性分类
$product_attribute_taxonomies=数组('pa_country'、'pa_class'、'pa_party'、'pa_gender');
$attr_output=array();//正在初始化
//循环浏览已定义的产品属性分类
foreach($product\U属性\U分类法为$TAXINOLY){
如果(分类法_存在($taxonomy)){
$label\u name=wc\u attribute\u label($taxonomy,$product);
$term\u names=$product->get\u属性($taxonomy);
如果(!空($term_names)){
$attr_output[]='.$label_name.:'.$term_name.';
}
}
}
//输出
回显“”。内爆(“
”,$attr\u输出)。“”;
}
代码进入活动子主题(或活动主题)的functions.php文件。已测试并运行。您的代码中有一些小错误。请尝试以下操作:
add_action('woocommerce_after_shop_loop_item_title', 'display_shop_loop_product_attributes');
function display_shop_loop_product_attributes() {
global $product;
// Define you product attribute taxonomies in the array
$product_attribute_taxonomies = array( 'pa_country', 'pa_class', 'pa_faction', 'pa_gender' );
$attr_output = array(); // Initializing
// Loop through your defined product attribute taxonomies
foreach( $product_attribute_taxonomies as $taxonomy ){
if( taxonomy_exists($taxonomy) ){
$label_name = wc_attribute_label( $taxonomy, $product );
$term_names = $product->get_attribute( $taxonomy );
if( ! empty($term_names) ){
$attr_output[] = '<span class="'.$taxonomy.'">'.$label_name.': '.$term_names.'</span>';
}
}
}
// Output
echo '<div class="product-attributes">'.implode( '<br>', $attr_output ).'</div>';
}
add_action('woocommerce_after_shop_loop_item_title', 'display_shop_loop_product_attributes');
function display_shop_loop_product_attributes() {
global $product;
// Only for simple products
if ( ! $product->is_type( 'simple' ) ) return;
// Define you product attribute taxonomies in the array
$product_attribute_taxonomies = array( 'pa_country', 'pa_class', 'pa_faction', 'pa_gender' );
$attr_output = array(); // Initializing
// Loop through your defined product attribute taxonomies
foreach( $product_attribute_taxonomies as $taxonomy ){
if( taxonomy_exists($taxonomy) ){
$label_name = wc_attribute_label( $taxonomy, $product );
$term_names = $product->get_attribute( $taxonomy );
if( ! empty($term_names) ){
$attr_output[] = '<span class="'.$taxonomy.'">'.$label_name.': '.$term_names.'</span>';
}
}
}
// Output
echo '<div class="product-attributes">'.implode( '<br>', $attr_output ).'</div>';
}
add_action('woocommerce_在_shop_loop_item_title'之后,'display_shop_loop_product_attributes');
功能显示\车间\循环\产品\属性(){
全球$产品;
//在数组中定义产品属性分类
$product_attribute_taxonomies=数组('pa_country'、'pa_class'、'pa_party'、'pa_gender');
$attr_output=array();//正在初始化
//循环浏览已定义的产品属性分类
foreach($product\U属性\U分类法为$TAXINOLY){
如果(分类法_存在($taxonomy)){
$label\u name=wc\u attribute\u label($taxonomy,$product);
$term\u names=$product->get\u属性($taxonomy);
如果(!空($term_names)){
$attr_output[]='.$label_name.:'.$term_name.';
}
}
}
//输出
回显“”。内爆(“
”,$attr\u输出)。“”;
}
代码进入活动子主题(或活动主题)的functions.php文件。测试和工作
仅对于简单产品,您将使用以下选项:
add_action('woocommerce_after_shop_loop_item_title', 'display_shop_loop_product_attributes');
function display_shop_loop_product_attributes() {
global $product;
// Define you product attribute taxonomies in the array
$product_attribute_taxonomies = array( 'pa_country', 'pa_class', 'pa_faction', 'pa_gender' );
$attr_output = array(); // Initializing
// Loop through your defined product attribute taxonomies
foreach( $product_attribute_taxonomies as $taxonomy ){
if( taxonomy_exists($taxonomy) ){
$label_name = wc_attribute_label( $taxonomy, $product );
$term_names = $product->get_attribute( $taxonomy );
if( ! empty($term_names) ){
$attr_output[] = '<span class="'.$taxonomy.'">'.$label_name.': '.$term_names.'</span>';
}
}
}
// Output
echo '<div class="product-attributes">'.implode( '<br>', $attr_output ).'</div>';
}
add_action('woocommerce_after_shop_loop_item_title', 'display_shop_loop_product_attributes');
function display_shop_loop_product_attributes() {
global $product;
// Only for simple products
if ( ! $product->is_type( 'simple' ) ) return;
// Define you product attribute taxonomies in the array
$product_attribute_taxonomies = array( 'pa_country', 'pa_class', 'pa_faction', 'pa_gender' );
$attr_output = array(); // Initializing
// Loop through your defined product attribute taxonomies
foreach( $product_attribute_taxonomies as $taxonomy ){
if( taxonomy_exists($taxonomy) ){
$label_name = wc_attribute_label( $taxonomy, $product );
$term_names = $product->get_attribute( $taxonomy );
if( ! empty($term_names) ){
$attr_output[] = '<span class="'.$taxonomy.'">'.$label_name.': '.$term_names.'</span>';
}
}
}
// Output
echo '<div class="product-attributes">'.implode( '<br>', $attr_output ).'</div>';
}
add_action('woocommerce_在_shop_loop_item_title'之后,'display_shop_loop_product_attributes');
功能显示\车间\循环\产品\属性(){
全球$产品;
//仅适用于Simple