为了账号安全,请及时绑定邮箱和手机立即绑定

在 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 关注
  • 153 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号