Woocommerce html产品描述或带有产品ID的简短描述简码

时间:2020-07-01 12:11:33

标签: php wordpress woocommerce product shortcode

我想显示另一个woocommerce产品的相同产品描述。因此,创建一个简短代码,以使简短代码按产品进行描述

function get_product_Des( $atts, $content = null )
{
    extract(shortcode_atts(array(
        'product_id' => ''
       ), $atts));
    
    $ReturnValue    = "";
    $product        = wc_get_product( $product_id );
    if($product) $ReturnValue = $product->get_description('edit');
    return($ReturnValue);
}


add_shortcode('getproductdes', 'get_product_Des');

但是结果是,短代码与原始主要描述不同。简码仅显示文本,它删除了

和一些html代码,无论如何我都可以使用相同的html代码获得相同的描述。

1 个答案:

答案 0 :(得分:0)

尝试以下重新访问的简码,以显示html格式的已定义产品ID的产品描述(或产品简短描述):

add_shortcode('product_descr', 'display_product_description');
function display_product_description( $atts )
{
    extract(shortcode_atts( array(
        'post_id' => '',
        'short'   => '0' // '1' for short description
    ), $atts));

    $content = $short == 0 ? get_the_content(null, false, $post_id) : get_the_excerpt( $post_id );

    return wpautop($content);
}

代码进入活动子主题(或活动主题)的functions.php文件中。经过测试,可以正常工作。

示例用法:

  • 有关产品描述:[product_descr post_id="37"]
  • 有关产品简短说明:[product_descr post_id="37" short="1"]