2016-02-07 30 views
0

我正在使用以下過濾器爲名稱='srtby_homepage'的參數設置url的一些參數。將變量數組傳遞給URL parmaters

$runorders = 'srtby_homepage'; 
    add_filter('acf/fields/relationship/query/name='.$runorders, 'relationship_options_filter', 10, 3); 

我試圖使用值的數組來代替這個喜歡以下,但認爲靜脈出了問題的地方

$runorders = array(
     'srtby_homepage', 
     'srtby_featuredops', 
    ); 
    add_filter('acf/fields/relationship/query/name='.$runorders, 'relationship_options_filter', 10, 3); 

這裏是如何我目前做的事情,對不同的$ runorders 2個功能:

$runorders = 'srtby_homepage'; 
    add_filter('acf/fields/relationship/query/name='.$runorders, 'relationship_options_filter', 10, 3); 

    function relationship_options_filter($options, $field, $the_post) { 

     $options['post_status'] = array('publish'); 

     return $options; 
    } 

    $runorders = 'srtby_featuredops'; 
    add_filter('acf/fields/relationship/query/name='.$runorders, 'relationship_options_filter_2', 10, 3); 

    function relationship_options_filter_2($options, $field, $the_post) { 

     $options['post_status'] = array('publish'); 

     return $options; 
    }; 
+1

你使用wrodpress嗎? –

+0

是的,那就對了 – James

+0

你想爲數組完成什麼查詢? – silver

回答

1

您可以按[]在GET請求發送陣列像foo[]=bar&foo[]=bar2&...

$runorders = array(
     'srtby_homepage', 
     'srtby_featuredops', 
    ); 
foreach($runorders as $key=>$value){ 
    $runorders[$key] = 'name[]='.urlencode($value); 
} 
$runorders = implode('&',$runorders); 
    add_filter('acf/fields/relationship/query/'.$runorders, 'relationship_options_filter', 10, 3); 

或者如果此字符串不是Get請求!您可以通過Dellimiter像,
加入陣列通過implode(',',$array);
並採用關鍵值由explode(',',$string);

+0

嘗試過但沒有歡樂 – James

+0

我不知道你有這個代碼在哪裏使用。但我的解決方案是標準的,您可以將數組轉換爲字符串或將其反轉。 –

+0

我也認爲你的解決方案會起作用,這很有道理。我已更新我的問題以顯示更多代碼 – James

0

第一次讀到它的一切與你選擇陣列製成的查詢字符串,然後拼接此字符串您的網址

<?php 
$runorders = array(
     'key1' => 'srtby_homepage', 
     'key2' => 'srtby_featuredops', 
    ); 
$query_string = ''; 
foreach($runorders as $key => $val) 
{ 
    $query_string .= '&'.$key.'='.$val; 
} 
$url = 'www.example.com/page.php?'.$query_string; 
?>