我想展示简短的产品描述和附加信息在我的wordpress的正常帖子中。是否有任何方法通过短代码或从PHP
没有内置的短代码来做到这一点,但很容易添加一个。在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' );