将相关的产品类别说明附加到Woocommerce中的产品说明中

时间:2018-03-20 18:15:08

标签: php wordpress woocommerce product wordpress-hook

我无法在每个产品的每个产品的每个产品的每个产品的每个产品的每个产品的每个产品的描述末尾添加产品的类别说明。到目前为止,我的函数(在底部)放置了所有类别描述,而不是产品实际所在的一个类别。

带描述页面的类别的

Here's the example

Sample product page with description (从:Stories美国原住民的故事和传说传统上起作用......)

这是我用来组合两个函数的代码。它可以添加所有类别描述,但我试图让它只显示一个相关的描述:

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;
}

https://pastebin.com/GHBxe23i

到目前为止,我在wordpress的非破坏性php插件中实现了代码。

1 个答案:

答案 0 :(得分:2)

您应该通过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文件。经过测试并正常工作。

相关问题