how to display product description of woo commerce

2019-06-11 10:28发布

i want to show short product description and additional information in my normal post of wordpress of woo commerce products.is there any way of doing this by short codes or from php in single post file in wordpress

1条回答
冷血范
2楼-- · 2019-06-11 11:30

There is not a built in shortcode to do this, but it is easy enough to add one. In the functions.php file (preferably in your Child Theme), add the following to create a shortcode like [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' );
查看更多
登录 后发表回答