我正在嘗試為 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 關注
- 169 瀏覽
添加回答
舉報
0/150
提交
取消