我正在尝试为 wordpress 创建一个简单的插件小部件,就像这样<?php// The widget classclass My_Custom_Widget extends WP_Widget { // Main constructor public function __construct() { parent::__construct( 'my_custom_widget', __( 'My Custom Widget', 'text_domain' ), array( 'customize_selective_refresh' => true, ) ); } // The widget form (for the backend ) public function form( $instance) {} // Update widget settings public function update($new_instance, $old_instance) {} public function helloWorld(){ echo 'Hello World'; } // Display the widget public function widget( $args, $instance ) { helloWorld(); }}// Register the widgetfunction my_register_custom_widget() { register_widget( 'My_Custom_Widget' );}add_action( 'widgets_init', 'my_register_custom_widget' );您会看到在函数小部件内部我调用了函数 helloWorld(),但是在显示这样的小部件时出现错误致命错误:在第 38 行调用 /wp-content/plugins/my-widget-plugin/my-widget-plugin.php 中的未定义函数 helloWorld()为什么我不能在函数内部调用函数?
1 回答

慕哥6287543
TA贡献1831条经验 获得超10个赞
你忘了添加$this:
// Display the widget
public function widget( $args, $instance ) {
$this->helloWorld();
}
希望能帮到你。
- 1 回答
- 0 关注
- 153 浏览
添加回答
举报
0/150
提交
取消