2015-05-29 77 views
0

我在WordPress中有以下代碼,以嘗試和實現一個階段,我可以使用esc_url(get_permalink() . '?month=' . get_query_var('month'));WordPress的 - 傳遞一個類的方法到query_var掛鉤

在試圖更多地瞭解WordPress和製作類,讓我增加更多的值,當我需要他們,我創建了下面的類,並將其鏈接到query_vars過濾:

//Create the needed GET vars // 

$custom_query_values = array('month','day'); 

new _custom_query_vars($custom_query_values); 

class _custom_query_vars 
{ 
    public $_custom_vars; 
    function __construct($custom_vars){ 
     $this->_custom_vars = $custom_vars; 
     add_filter('query_vars',array(&$this, '_add_custom_querys')); 
    } 

    public function _add_custom_querys(){ 
    // Return an array of values // 
     foreach($this->_custom_vars as $value) 
     { 
      $vars[] = $value; 
     } 
     print_r($vars); 
     return $vars; 
    } 
} 

/*function add_custom_query_var($vars){ 
    $vars[] = "month"; 
    $vars[] = "day"; 
    return $vars; 
} 
add_filter('query_vars', 'add_custom_query_var'); */ 

上面的代碼不起作用,相反,當類是活動的,當我創建一個新的實例時,我網站上的所有頁面都將停止工作,我將簡單地指向我的根地址。但是,函數「似乎」正在工作,因爲print_r()的確會打印Array ([0] => month [1] => day)的值,因此該方法必須以某種形式或形式傳遞給query_var掛鉤。

被註釋掉的代碼的第二部分是我試着簡單地返回靜態值的標準函數。這工作正常,並按照預期使用正常的esc_url(get_permalink() . '?month=' . get_query_var('month'));工作。有任何想法嗎? (最後一件事,有沒有辦法讓這個http://www.sitename/pagename/month)。

感謝您的任何和所有幫助,

+0

公共職能_add_custom_querys(){ 的print_r($這個 - > _ custom_vars); } 什麼打印。 –

+0

嗨Dev Danidhariya,輸出*是數組([0] => month [1] => day) –

回答

0

我不知道,但試試這個。

public function _add_custom_querys(){ 
     $vars = Array(); 

     foreach($this->_custom_vars as $value){ 
      array_push($vars,$value); 
     } 
      return $vars; 
} 
+0

嗨開發Danidhariya,可悲的是沒有工作。我得到同樣的錯誤,即print_r仍然可以工作,但網頁只顯示我的主頁(索引) –