【问题标题】:Function for adding a shortcode to product template not working向产品模板添加简码的功能不起作用
【发布时间】:2017-04-24 01:05:33
【问题描述】:

所以我需要在 Woocommerce 的每个产品页面中添加一个简码 [stag_sidebar id='product']。特别是简短的描述。这是我尝试过的,但这不起作用:

add_filter( 'woocommerce_short_description', 'custom_woocommerce_short_description' );

function custom_woocommerce_short_description( $description ) {
global $post;

$description = $description . do_shortcode('[stag_sidebar id='product']') ;

return $description;
}

【问题讨论】:

  • 它不起作用是什么意思?问题是什么?会发生什么?
  • 页面刚被冻结。白屏。我注意到当我将这样的简码: [testshortcode] 放入函数中时,页面正在正常加载。
  • 你得到一个白屏,因为你有一个 php 错误,我在下面回答。 :)

标签: php wordpress woocommerce hook-woocommerce


【解决方案1】:

您的 do_shortcode 函数有错误,您使用了两次单引号。您应该转义它们,或者交替使用单引号和双引号,如下所示:

$description = $description . do_shortcode("[stag_sidebar id='product']") ;

【讨论】:

  • 谢谢,现在页面通过了,但似乎短代码的值没有显示在单个产品页面上..
  • 如果您在产品单页的内容中添加短代码是否有效?也许短代码没有正确定义。
  • 定义它的方式是什么?
  • 应该用 add_shortcode 函数定义:codex.wordpress.org/Function_Reference/add_shortcode。如果它在内容中有效,我真的不知道是什么问题,你的代码看起来很好。
猜你喜欢
  • 2023-02-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-27
  • 1970-01-01
  • 1970-01-01
  • 2023-03-14
  • 1970-01-01
相关资源
最近更新 更多