首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Woocommerce中将相关产品类别描述附加到产品描述中

在Woocommerce中将相关产品类别描述附加到产品描述中
EN

Stack Overflow用户
提问于 2018-03-20 18:15:08
回答 1查看 763关注 0票数 2

在每一项下的“描述”选项卡中,在每个产品的每个描述的末尾添加我的产品的类别描述是有问题的。到目前为止,我的功能(在底部)把所有的类别描述,而不是产品的实际类别。

带有描述页面的类别的下面是一个例子

产品说明样例页 (开始于:美国土著故事和传说传统上的服务....)

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

代码语言:javascript
复制
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插件实现代码。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-03-20 20:22:34

您应该在代码get_terms()中用wp_get_post_terms()替换如下:

代码语言:javascript
复制
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文件中。测试和工作。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49391466

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档