将相关产品类别描述附加到 Woocommerce 中的产品描述
Append related product categories description to product description in Woocommerce
我无法在每个项目下方的 'description' 选项卡中为每个产品的每个描述的末尾添加我的产品的类别描述。到目前为止,我的功能(在底部)放置了所有类别描述,而不是产品实际所在的类别。
Here's the example 类别的描述页面。
Sample product page with description (开始于:美国原住民的故事和传说传统上服务于... ...)
这是我用来组合两个函数的代码。它可以添加所有类别描述,但我试图让它只显示一个相关描述:
add_filter( 'the_content', 'customizing_woocommerce_description' );
function customizing_woocommerce_description( $content ) {
// Only for single product pages (woocommerce)
if ( is_product() ) {
// The custom content
$args = array( 'taxonomy' => 'product_cat' );
$terms = get_terms('product_cat', $args);
$count = count($terms);
// if ($count > 0) {
foreach ($terms as $term) {
/* echo $term->description;*/
$custom_content = '<p class="custom-content">' . __($term->description, "woocommerce").'</p>';
// Inserting the custom content at the end
$content .= $custom_content;
}
// }
}
return $content;
}
到目前为止,我在 wordpress 的非破坏性 php 插件中实现代码。
您应该在代码中将 get_terms()
替换为 wp_get_post_terms()
:
add_filter( 'the_content', 'woocommerce_custom_product_description', 20, 1 );
function woocommerce_custom_product_description( $content ) {
global $post;
// Only for woocommerce single product pages
if ( ! is_product() )
return $content; // exit
$terms = wp_get_post_terms( $post->ID, 'product_cat' );
if ( count( $terms ) == 0 )
return $content; // exit
foreach ( $terms as $term )
$content .= '<p class="custom-content">' . $term->description . '</p>';
return $content;
}
代码进入您的活动子主题(或主题)的 function.php 文件。 已测试并有效。
我无法在每个项目下方的 'description' 选项卡中为每个产品的每个描述的末尾添加我的产品的类别描述。到目前为止,我的功能(在底部)放置了所有类别描述,而不是产品实际所在的类别。
Here's the example 类别的描述页面。
Sample product page with description (开始于:美国原住民的故事和传说传统上服务于... ...)
这是我用来组合两个函数的代码。它可以添加所有类别描述,但我试图让它只显示一个相关描述:
add_filter( 'the_content', 'customizing_woocommerce_description' );
function customizing_woocommerce_description( $content ) {
// Only for single product pages (woocommerce)
if ( is_product() ) {
// The custom content
$args = array( 'taxonomy' => 'product_cat' );
$terms = get_terms('product_cat', $args);
$count = count($terms);
// if ($count > 0) {
foreach ($terms as $term) {
/* echo $term->description;*/
$custom_content = '<p class="custom-content">' . __($term->description, "woocommerce").'</p>';
// Inserting the custom content at the end
$content .= $custom_content;
}
// }
}
return $content;
}
到目前为止,我在 wordpress 的非破坏性 php 插件中实现代码。
您应该在代码中将 get_terms()
替换为 wp_get_post_terms()
:
add_filter( 'the_content', 'woocommerce_custom_product_description', 20, 1 );
function woocommerce_custom_product_description( $content ) {
global $post;
// Only for woocommerce single product pages
if ( ! is_product() )
return $content; // exit
$terms = wp_get_post_terms( $post->ID, 'product_cat' );
if ( count( $terms ) == 0 )
return $content; // exit
foreach ( $terms as $term )
$content .= '<p class="custom-content">' . $term->description . '</p>';
return $content;
}
代码进入您的活动子主题(或主题)的 function.php 文件。 已测试并有效。