2013-07-18 44 views
0

我有一個WordPress小部件,但我不知道如何從form函數的類下面的my_custom函數獲取$ title變量。我想這個新手解決方案,但沒有工作如何獲取函數的變量

class My_Widget extends WP_Widget { 

public function __construct() { 

} 

public function widget($args, $instance) { 

} 

public function form($instance) { 

     if (isset($instance[ 'title' ])) { 
     $title = $instance[ 'title' ]; 
    } 
    else { 
    $title = 'New title'; 

} 
    } 
public function update($new_instance, $old_instance) { 

} 
    } 

    function my_custom(){ 

    $my_title = $instance[ 'title' ]; 
     echo $my_title; 

    } 
+0

缺少一個右}否則以後在形式上功能 – Orangepill

+0

$標題應分配的財產'$ this-> title =「新標題」;'在表格功能 – Orangepill

回答

0

先類的一個對象,然後獲取類變量

class My_Widget extends WP_Widget { 
public $mytitle; 
public function __construct() { 

} 

public function widget($args, $instance) { 

} 

public function form($instance) { 

     if (isset($instance[ 'title' ])) { 
     $this->mytitle = $instance[ 'title' ]; 
    } 
    else { 
    $this->mytitle = 'New title'; 

} 
    } 
public function update($new_instance, $old_instance) { 

} 
    } 

    function my_custom(){ 
    $widgetobj=new My_Widget(); 
    $my_title = $widgetobj->mytitle; 
     echo $my_title; 

    } 
+0

仍然不適合我? – Zaki