为什么我收到此致命错误消息[重复]

问题描述 投票:1回答:1

这个问题在这里已有答案:

我为woocommerce编写了自己的短代码。

该代码在content-single-product.php上运行良好。但是,任何其他页面,我的网站中断。为什么?

Fatal error: Call to a member function get_gallery_image_ids() on null in /mysite/wp-content/plugins/woocommerce/templates/single-product/product-thumbnails.php on line 27

这是我写的功能:

/* My custom shortcode code*/
function get_woocommerce_gallery_image_thumbnails ( $atts ) {

     // Buffer our contents
     ob_start();

     /* Line to get the gallery thumbnails */
wc_get_template( 'single-product/product-thumbnails.php');

     // Return buffered contents
     return ob_get_clean();
}

add_shortcode( 'wc_get_thumbs', 'get_woocommerce_gallery_image_thumbnails' );
php woocommerce runtime-error
1个回答
0
投票

根据给定的信息,每个页面都要求*something*->get_gallery_image_ids(),但*something*仅存在于您的产品页面上。查看可用数据,这个*something*可能是在product-thumbnails模板中创建的。

根据this answer的信息,我们可以看到*something*实际上是global $product,这是product-thumbnails所必需的。

我想,最好的解决方案是从每个不是产品页面的页面中删除product-thumbnails

作为替代方案,您可以在短代码定义中添加安全措施:

function get_woocommerce_gallery_image_thumbnails ( $atts ) {
     global $product;
     if (!isset($product)) {
         return;
     }

     // Buffer our contents
     ob_start();

     /* Line to get the gallery thumbnails */
     wc_get_template( 'single-product/product-thumbnails.php');

     // Return buffered contents
     return ob_get_clean();
}
© www.soinside.com 2019 - 2024. All rights reserved.