CRITICAL Uncaught Error: Call to a member function get_description() on bool (woocommerce)
CRITICAL Uncaught Error: Call to a member function get_description() on bool (woocommerce)
我需要为我的产品获取一个 [product_description] 短代码(它们具有特定的结构,标签不适合我)所以将这段代码添加到我的 functios 文件中
add_shortcode( 'custom_product_description', 'display_custom_product_description' );
function display_custom_product_description( $atts ){
$atts = shortcode_atts( array(
'id' => get_the_id(),
), $atts, 'custom_product_description' );
global $product;
if ( ! is_a( $product, 'WC_Product') )
$product = wc_get_product($atts['id']);
return $product->get_description();
}
不过,工作完美,符合我的要求;自从我得到这段代码后,我就在日志中收到了这个致命错误:
2021-03-16T00:00:16+00:00 CRITICAL Uncaught Error: Call to a member function get_description() on bool in /home/evadevcl/public_html/elr/wp-content/themes/flatsome-child/functions.php:30
Stack trace:
#0 /home/evadevcl/public_html/elr/wp-includes/shortcodes.php(343): display_custom_product_description(Array, '', 'custom_product_...')
#1 [internal function]: do_shortcode_tag(Array)
#2 /home/evadevcl/public_html/elr/wp-includes/shortcodes.php(218): preg_replace_callback('/\[(\[?)>>>>(custom...', 'do_shortcode_ta...', '\n[custom_produc...')
#3 /home/evadevcl/public_html/elr/wp-content/themes/flatsome/inc/helpers/helpers-shortcode.php(203): do_shortcode('\n[custom_produc...')
#4 /home/evadevcl/public_html/elr/wp-content/themes/flatsome/inc/shortcodes/row.php(214): flatsome_contentfix('\n[custom_produc...')
#5 /home/evadevcl/public_html/elr/wp-includes/shortcodes.php(343): ux_col(Array, '\n[custom_produc...', 'col')
#6 [internal function]: do_shortcode_tag(Array)
#7 /home/evadevcl/public_html/elr/wp-includes/shortcodes.php(218): preg_replace_callback( en /home/evadevcl/public_html/elr/wp-content/themes/flatsome-child/functions.php en la línea 30
即使我搜索了这个确切的问题,我似乎也找不到解决方案,它不会对我的网站造成任何伤害(可见或功能),但我担心这可能会阻止我的网站任何时候,有人可以帮助我吗?
您可以添加初始检查以验证简码仅在产品页面上激活。
add_shortcode( 'custom_product_description', 'display_custom_product_description' );
function display_custom_product_description( $atts ){
// only on the product page
if ( ! is_product() ) {
return;
}
$atts = shortcode_atts( array(
'id' => get_the_id(),
), $atts, 'custom_product_description' );
global $product;
if ( ! is_a( $product, 'WC_Product') )
$product = wc_get_product($atts['id']);
return $product->get_description();
}
如果你想在产品页面之外也使用简码
您不必将 get_the_id()
值设置为第一个参数的 id 属性,否则简码将始终忽略用户输入的产品 ID .
You can use the following shortcode on any page of the site by
setting, in this case, the id attribute with the id of the product you
want to get the description of.
您可以像这样优化简码:
add_shortcode( 'custom_product_description', 'display_custom_product_description' );
function display_custom_product_description( $atts ){
$atts = shortcode_atts( array(
'id' => '',
), $atts, 'custom_product_description' );
if ( empty( $atts['id'] ) ) {
if ( is_product() ) {
global $product;
} else {
return;
}
} else {
if ( is_a( wc_get_product($atts['id']), 'WC_Product' ) ) {
$product = wc_get_product( $atts['id'] );
} else {
return;
}
}
return $product->get_description();
}
我需要为我的产品获取一个 [product_description] 短代码(它们具有特定的结构,标签不适合我)所以将这段代码添加到我的 functios 文件中
add_shortcode( 'custom_product_description', 'display_custom_product_description' );
function display_custom_product_description( $atts ){
$atts = shortcode_atts( array(
'id' => get_the_id(),
), $atts, 'custom_product_description' );
global $product;
if ( ! is_a( $product, 'WC_Product') )
$product = wc_get_product($atts['id']);
return $product->get_description();
}
不过,工作完美,符合我的要求;自从我得到这段代码后,我就在日志中收到了这个致命错误:
2021-03-16T00:00:16+00:00 CRITICAL Uncaught Error: Call to a member function get_description() on bool in /home/evadevcl/public_html/elr/wp-content/themes/flatsome-child/functions.php:30 Stack trace: #0 /home/evadevcl/public_html/elr/wp-includes/shortcodes.php(343): display_custom_product_description(Array, '', 'custom_product_...') #1 [internal function]: do_shortcode_tag(Array) #2 /home/evadevcl/public_html/elr/wp-includes/shortcodes.php(218): preg_replace_callback('/\[(\[?)>>>>(custom...', 'do_shortcode_ta...', '\n[custom_produc...') #3 /home/evadevcl/public_html/elr/wp-content/themes/flatsome/inc/helpers/helpers-shortcode.php(203): do_shortcode('\n[custom_produc...') #4 /home/evadevcl/public_html/elr/wp-content/themes/flatsome/inc/shortcodes/row.php(214): flatsome_contentfix('\n[custom_produc...') #5 /home/evadevcl/public_html/elr/wp-includes/shortcodes.php(343): ux_col(Array, '\n[custom_produc...', 'col') #6 [internal function]: do_shortcode_tag(Array) #7 /home/evadevcl/public_html/elr/wp-includes/shortcodes.php(218): preg_replace_callback( en /home/evadevcl/public_html/elr/wp-content/themes/flatsome-child/functions.php en la línea 30
即使我搜索了这个确切的问题,我似乎也找不到解决方案,它不会对我的网站造成任何伤害(可见或功能),但我担心这可能会阻止我的网站任何时候,有人可以帮助我吗?
您可以添加初始检查以验证简码仅在产品页面上激活。
add_shortcode( 'custom_product_description', 'display_custom_product_description' );
function display_custom_product_description( $atts ){
// only on the product page
if ( ! is_product() ) {
return;
}
$atts = shortcode_atts( array(
'id' => get_the_id(),
), $atts, 'custom_product_description' );
global $product;
if ( ! is_a( $product, 'WC_Product') )
$product = wc_get_product($atts['id']);
return $product->get_description();
}
如果你想在产品页面之外也使用简码
您不必将 get_the_id()
值设置为第一个参数的 id 属性,否则简码将始终忽略用户输入的产品 ID .
You can use the following shortcode on any page of the site by setting, in this case, the id attribute with the id of the product you want to get the description of.
您可以像这样优化简码:
add_shortcode( 'custom_product_description', 'display_custom_product_description' );
function display_custom_product_description( $atts ){
$atts = shortcode_atts( array(
'id' => '',
), $atts, 'custom_product_description' );
if ( empty( $atts['id'] ) ) {
if ( is_product() ) {
global $product;
} else {
return;
}
} else {
if ( is_a( wc_get_product($atts['id']), 'WC_Product' ) ) {
$product = wc_get_product( $atts['id'] );
} else {
return;
}
}
return $product->get_description();
}