Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用PHP和wordpress使用面向对象编程实现短代码?_Php_Wordpress - Fatal编程技术网

如何使用PHP和wordpress使用面向对象编程实现短代码?

如何使用PHP和wordpress使用面向对象编程实现短代码?,php,wordpress,Php,Wordpress,我正在尝试使用PHP OOP构建一个插件,但我似乎无法实现这第一步。这个代码有问题吗?当我把短代码[rsb archive]放入wordpress页面时,所有这些都不起作用 代码如下: class RSB_Archive { public function __construct() { add_shortcode('rsb-archive', array($this, 'shortcode')); } public function short

我正在尝试使用PHP OOP构建一个插件,但我似乎无法实现这第一步。这个代码有问题吗?当我把短代码[rsb archive]放入wordpress页面时,所有这些都不起作用

代码如下:

class RSB_Archive {
    public function __construct()
    {
        add_shortcode('rsb-archive', array($this, 'shortcode'));
    }

    public function shortcode()
    {
           // Contents of this function will execute when the blogger
          // uses the [rsb-archive] shortcode.

          echo "print this code";



    }
}
这似乎不起作用。有什么想法吗

此外,是否有人对如何使用PHP调试代码有任何见解。我刚刚开始学习如何使用它


谢谢

您必须首先创建小部件对象的实例

就这样,

<?php

class RSB_Archive {

    public function __construct() {
       // don't call add_shortcode here
       // actually, I worked with wordpress last year and
       // i think this is a good place to call add_shortcode 
       // (and all other filters) now...
    }

    public function shortcode() {
        echo 'doTheDoo';
    }
}

$myWidged = new RSB_Archive();

add_shortcode('my-code', array($myWidget, 'shortcode'));

?>

您忘记创建类RSB_归档的实例。
构造函数中的代码在创建对象时运行

class RSB_Archive {

public function __construct()
{
    add_shortcode('rsb-archive', array($this, 'shortcode'));
}

public function shortcode()
{
       // Contents of this function will execute when the blogger
      // uses the [rsb-archive] shortcode.

      echo "print this code";



   }
}

$obj = new RSB_Archive();

您在哪里分配执行此操作的小部件实例?短代码正在放置到wordpress页面中,但是,我不太理解您的问题。您只是调用
$RSB_Archive=new RSB_Archive?很抱歉,我不理解你的问题。你不能把代码放在任何地方,希望它执行,你必须实例化这个类来启动构造函数。