Wordpress - 从短代码函数返回类变量/函数?

时间:2016-02-24 03:04:13

标签: wordpress

如何从短代码方法返回类变量/函数?

class MyClass
{
    private $content;

    function MyClass()
    {
        $this->content= "value";
    }

    public function getContent()
    {
        ob_start();
        print "This is content: ". $this->content;
        $output = ob_get_contents();
        ob_end_clean();
        return $this->content;
    }
}
add_shortcode('architects-side', array( 'MyClass', 'getContent' )); // not work

问题是无效,因为我在$this->content中使用getContent(),在getContent()中使用变量/函数的正确方法是什么?

1 个答案:

答案 0 :(得分:1)

我没有测试它,但我认为这可行:

class MyClass
{
    private $content;

    function MyClass()
    {
        $this->content= "value";
        add_shortcode('architects-side', array( $this, 'getContent' ));
    }

    public function getContent()
    {
        ob_start();
        print "This is content: ". $this->content;
        $output = ob_get_contents();
        ob_end_clean();
        return $this->content;
    }
}
$myClass = new MyClass();