我们的WooCommerce产品有两个自定义分类:大小和颜色。
我试图在产品的单一产品页面上显示产品的自定义毒理学术语。我正在使用woocommerce_single_product_summary动作钩子。我不能同时显示两个分类法。
到目前为止,这是我的代码:
ID , array( 'sizes') );
// begin creating html markup
$size_markup = '';
if(!empty($size_terms)) {
$size_markup .= "Similar size shades: ";
}
// init counter
$is = 1;
foreach ( $size_terms as $size_term ) {
$size_markup .= '' . $size_term->name . '';
// Add comma (except after the last item)
$size_markup .= ($is < count($size_terms))? ", " : "";
// Increment counter
$is++;
//finish the markup
if(!empty($size_terms)) {
$size_markup .= "";
}
}
echo $size_markup;
};
function display_single_product_colors_after_summary() {
global $post;
$color_terms = get_the_terms( $post->ID , array( 'colors') );
// begin creating html markup
$color_markup = '';
if(!empty($color_terms)) {
$color_markup .= "Similar color shades: ";
}
// init counter
$ic = 1;
foreach ( $color_terms as $color_term ) {
$color_markup .= '' . $color_term->name . '';
// Add comma (except after the last item)
$color_markup .= ($ic < count($color_terms))? ", " : "";
// Increment counter
$ic++;
//finish the markup
if(!empty($color_terms)) {
$color_markup .= "";
}
}
echo $color_markup;
};
function display_single_product_terms_after_summary() {
display_single_product_sizes_after_summary();
display_single_product_colors_after_summary();
};
add_action( 'woocommerce_single_product_summary', 'display_single_product_terms_after_summary', 101, 0 );
?>这将产生以下结果:
相似大小的阴影:小而相似的色系:
如果我反转display_single_product_traits_after_summary中两个子函数的顺序,输出将更改为:
相似的颜色:蓝色,相似大小的阴影:
我试着在每个子函数的末尾使用重置。我试过:
echo $size_markup;
wp_reset_postdata();以及:
echo $color_markup;
wp_reset_postdata();这没什么区别。
我也试过:
echo $size_markup;
rewind_posts();以及:
echo $color_markup;
rewind_posts();这完全打破了这一页。
我在这里犯了什么错误,我如何才能从这两个分类法中得到术语来显示呢?
发布于 2020-07-05 23:05:27
WordPress文档显示,数组对于get_the_terms (https://developer.wordpress.org/reference/functions/get_这个_条款/)中的分类法来说是一个可接受的参数。如果您试图获得多个分类法,则数组可以工作。如果你只想得到一个分类法的话,它甚至会起作用。但是,顺序地获得两个单一的分类法数组是破坏这段代码的原因。
在最初发布的代码中,替换如下:
$size_terms = get_the_terms( $post->ID , array( 'sizes') );取而代之的是:
$size_terms = get_the_terms( $post->ID , 'sizes' );也取代了这一点:
$color_terms = get_the_terms( $post->id , array( 'colors') );取而代之的是:
$color_terms = get_the_terms( $post->id , 'colors' );解决了问题。
另一种可行的方法是同时检索这两组分类法术语,然后拆开WP_Term对象,找到您想要的内容。在大多数情况下,这可能是更好的解决方案。
function display_single_product_taxonomy_terms() {
global $post;
$tax_terms = get_the_terms( $post->ID , array( 'sizes','colors') );
// create the markup variables
$color_markup = '';
$size_markup = '';
// init counters
$color_counter = 0;
$size_counter = 0;
foreach ( $tax_terms as $tax_term ) {
if( $tax_term->taxonomy == 'colors' ) {
// this is a colors term; increment color counter
$color_counter++;
// add to color markup
$color_markup .= ($color_counter > 1)? "; " : "";
$color_markup .= '' . str_replace(", and,"," and",str_replace("-", ", ", $tax_term->slug)) . '';
} else {
// this is a sizes term; increment size counter
$size_counter++;
// add to size markup
$size_markup .= ($size_counter > 1)? "; " : "";
$size_markup .= '' . str_replace("-", " ", $tax_term->slug) . '';
}
};
echo '' . PHP_EOL;
if ( $size_counter > 0 ) {
echo ''. __("Similar size shades: ","bright-side") . $size_markup . '' . PHP_EOL;
};
if ( $color_counter > 0 ) {
echo '' . __("Similar color shades: ","bright-side") . $color_markup . '' . PHP_EOL;
};
echo '' . PHP_EOL;
};
add_action( 'woocommerce_single_product_summary', 'display_single_product_taxonomy_terms', 101, 0 );https://wordpress.stackexchange.com/questions/370316
复制相似问题