【问题标题】:how to display product description of woo commerce products in normal post of wordpress如何在wordpress的正常帖子中显示woo commerce产品的产品描述
【发布时间】:2014-10-17 22:45:39
【问题描述】:

我想在 woo commerce products 的 wordpress 的正常帖子中显示简短的产品描述和附加信息。有没有办法通过短代码或 php 来做到这一点 在 wordpress 中的单个帖子文件中

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    没有内置的短代码来执行此操作,但添加一个很容易。在functions.php 文件中(最好在您的Child Theme 中),添加以下内容以创建类似[product_shortdesc id="123"/] 的短代码。

    function product_shortdesc_shortcode( $atts ){
        // use shortcode_atts() to set defaults then extract() to variables
        extract( shortcode_atts( array( 'id' => false ), $atts ) );
    
        // if an $id was passed, and we could get a Post for it, and it's a product....
        if ( ! empty( $id ) && null != ( $product = get_post( $id ) ) && $product->post_type = 'product' ){
            // apply woocommerce filter to the excerpt
            echo apply_filters( 'woocommerce_short_description', $product->post_excerpt );
        }
    }
    // process [product_shortdesc] using product_shortdesc_shortcode()
    add_shortcode( 'product_shortdesc', 'product_shortdesc_shortcode' );
    

    【讨论】:

    • 它没有在所有帖子中显示简短描述。它仅适用于新创建的产品
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-22
    • 2018-04-05
    • 1970-01-01
    • 1970-01-01
    • 2021-10-09
    • 2019-07-29
    • 2020-08-18
    相关资源
    最近更新 更多