亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 wordpress 中創建自定義小部件

在 wordpress 中創建自定義小部件

PHP
慕標琳琳 2023-05-12 15:20:26
我正在嘗試為 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();

    }

希望能幫到你。


查看完整回答
反對 回復 2023-05-12
  • 1 回答
  • 0 關注
  • 169 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號