【问题标题】:How do I get the description from a woocommerce product?如何从 woocommerce 产品中获取描述?
【发布时间】:2016-10-19 20:33:47
【问题描述】:

我正在构建一个短代码以在通过 ID 传递时显示产品信息,我已经获得了标题和图像,但描述是我遇到问题的那个。

function wcproductslider($atts) {
$a = shortcode_atts( array(
    'product' => '98',
), $atts );

$product_ID = $a['product'];
$pro = new WC_Product($product_ID);

$ret = "<b>Title: </b>".$pro->get_title() . "<br>";

$ret .= "<b>Image: </b><br> <a href=' " .$pro->get_permalink() . " '>".$pro->get_image($size = 'shop_thumbnail') . "</a><br>"; 

$ret .= "<b>Long Description: </b>" . $pro->post->post_content;
$ret .= "<b>Short description: </b>" . $pro->post->post_excerpt;
return $ret;

}

我设法使用wc product class 解决了其中的一些问题,但没有关于产品摘录的任何内容。

任何想法将不胜感激。

【问题讨论】:

  • 我认为所有的post数据都存储在$pro-&gt;post,所以你也许可以使用$pro-&gt;post-&gt;post_excerpt
  • 尝试了它没有乐趣:/即使有括号,但这给了我一个错误

标签: wordpress woocommerce


【解决方案1】:

想通了,没有在我对产品的简短描述中添加任何内容。

详细说明

$pro->post->post_content;

简短说明

$pro->post->post_excerpt;

【讨论】:

  • 我建议使用以下apply_filters('the_content', $pro-&gt;post-&gt;post_content); 这将运行您在内容中拥有的任何短代码,自动嵌入任何视频,如果启用了wpautop,则应用段落标签。它基本上会完成您对the_content() 的所有期望
  • 运行the_content 过滤器可能如果您使用任何与之挂钩的插件也会给您带来麻烦。例如,我曾经在我的网站上共享图标。对于简短的内容,WooCommerce 有一个 woocommerce_template_single_excerpt() 函数,它通过它自己的 woocommerce_short_description 过滤器运行摘录,这大概模仿了在 the_content 上运行的大多数过滤器,而不会让自己陷入社交图标的疯狂。
猜你喜欢
  • 2013-11-14
  • 1970-01-01
  • 2018-07-27
  • 1970-01-01
  • 2016-04-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-09-23
相关资源
最近更新 更多