Php 在自定义主页和产品类别存档中显示WooCommerce产品属性
下面的代码显示产品类别归档页面上的产品属性,并基于“”应答线程 但是,当我在主页上添加该类别时,它不会显示属性 我的代码版本:Php 在自定义主页和产品类别存档中显示WooCommerce产品属性,php,wordpress,woocommerce,product,custom-taxonomy,Php,Wordpress,Woocommerce,Product,Custom Taxonomy,下面的代码显示产品类别归档页面上的产品属性,并基于“”应答线程 但是,当我在主页上添加该类别时,它不会显示属性 我的代码版本: // Show Attributes on Product Category Page add_action('woocommerce_after_shop_loop_item','display_loop_product_attribute' ); function display_loop_product_attribute() { global $pro
// Show Attributes on Product Category Page
add_action('woocommerce_after_shop_loop_item','display_loop_product_attribute' );
function display_loop_product_attribute() {
global $product;
//$product_attributes = array('pa_size', 'pa_color');
$product_attributes = array('pa_size');
$attr_output = array();
// Loop through the array of product attributes
foreach( $product_attributes as $taxonomy ){
if( taxonomy_exists($taxonomy) ){
if( $value = $product->get_attribute($taxonomy) ){
// The product attribute label name
$label_name = get_taxonomy($taxonomy)->labels->singular_name;
// Storing attributes for output
$attr_output[] = '<span class="'.$taxonomy.'">'.$label_name.': '.$value.'</span>';
}
}
}
// Output attribute name / value pairs separate by a "<br>"
echo '<div class="product-attributes-custom">'.implode('<br>', $attr_output).'</div>';
}
//在产品类别页面上显示属性
添加_操作(“在_店铺_循环_项目”之后添加商业_”,“显示_循环_产品_属性”);
函数显示\循环\产品\属性(){
全球$产品;
//$product_attributes=数组('pa_size'、'pa_color');
$product_attributes=数组('pa_size');
$attr_output=array();
//循环遍历产品属性数组
foreach($product_属性作为$TAXINORYOM){
如果(分类法_存在($taxonomy)){
如果($value=$product->get_属性($taxonomy)){
//产品属性标签名称
$label\u name=get\u taxonomy($taxonomy)->labels->singular\u name;
//存储输出属性
$attr_output[]='.$label_name.':'.$value.';
}
}
}
//以“
”分隔的输出属性名称/值对
回显“”。内爆(“
”,$attr\u输出)。“”;
}
我不知道如何使它在我的自定义主页上显示产品和特定的产品类别页面上工作。对于主页,我知道要使用的条件标记是
is_home()
…但如何使其也适用于产品类别页面?已更新-以下代码将在上显示特定的产品属性:
- 主页用于使用WordPress条件标记的自定义产品循环
- 使用Woocommerce条件标记定义产品类别存档页面
add_action('woocommerce_after_shop_loop_item','display_loop_product_attribute' );
function display_loop_product_attribute() {
global $product;
// The Product attribute to be displayed
$product_attributes = array('pa_size');
// The targeted Product category archive pages (slugs)
$categories = array('t-shirts', 'hoodies', 'socks');
$output = array(); // Initializing
// Targetting home page and specific product category archive pages
if( is_front_page() || is_product_category($categories) ) {
// Loop through the array of product attributes
foreach( $product_attributes as $taxonomy ){
if( taxonomy_exists($taxonomy) ){
if( $values = $product->get_attribute($taxonomy) ){
// The product attribute label name
$label_name = get_taxonomy($taxonomy)->labels->singular_name;
// Storing attributes for output
$output[] = '<span class="'.$taxonomy.'">'.$label_name.': '.$values.'</span>';
}
}
}
// Output attribute name / value pairs, separate by a "<br>"
echo '<div class="product-attributes-custom">'.implode('<br>', $output).'</div>';
}
}
以下是:
// convert string of term names to an array
$values = explode(', ', $values);
// Format each and set back to a string
$values = '<span>' . implode('</span> <span>', $values) . '</span>';
//将术语名称字符串转换为数组
$values=分解(“,”,$values);
//格式化每个文件并设置回字符串
$values=''。内爆(“”,$values)。“”;
相关:感谢@LoicTheAztec提供的解决方案,它的效果非常好!唯一的问题是你忘了在你的阵法入门中加上“,这很容易被发现。谢谢@LoicTheAztech,我已经接受了你的回答。还有一件事我想知道的是,我们有没有可能设计属性的样式?现在,它们都以这样的大小显示在单个元素中:欧元41/10.5-11 US,欧元42/11.5-12我如何在不同的跨度中添加每个大小,以便易于设置样式?@Jacob我已轻松更新了代码(变量重命名),并为您添加了一个新的(插入函数代码)这会满足你的要求。
// convert string of term names to an array
$values = explode(', ', $values);
// Format each and set back to a string
$values = '<span>' . implode('</span> <span>', $values) . '</span>';