WordPress Eventbrite嵌入式结账插件404页报错及代码修改咨询
问题解决思路与代码修正
错误原因
404页面不存在有效的WP_Post对象,原代码直接调用$post->post_content时,$post为空,因此抛出“Trying to get property 'post_content' of non-object”的提示。
修正方案
不需要替换post_content,核心是先确保$post是有效的文章对象,再执行短代码检查。修改后的完整函数如下:
function wpeec_external_js(){ global $post; // 先验证$post是否为有效WP_Post实例,避免无文章页面报错 if ( isset( $post ) && is_a( $post, 'WP_Post' ) && has_shortcode( $post->post_content, 'wp-eventbrite-checkout' ) ): wp_enqueue_script( 'wpeec_eb_widgets', 'https://www.eventbrite.com/static/widgets/eb_widgets.js', true ); endif; } add_action( 'wp_enqueue_scripts', 'wpeec_external_js' );
模态弹窗场景适配
如果你的模态弹窗短代码并非直接写在文章内容里,而是通过主题模板、自定义字段等方式加载,可以直接强制加载脚本,确保弹窗功能正常:
function wpeec_external_js(){ // 强制加载脚本,适配模态弹窗场景 wp_enqueue_script( 'wpeec_eb_widgets', 'https://www.eventbrite.com/static/widgets/eb_widgets.js', true ); } add_action( 'wp_enqueue_scripts', 'wpeec_external_js' );
内容的提问来源于stack exchange,提问作者Dabin Kim
相关产品推荐
相关产品推荐

