将变量数组传递给URL parmaters

时间:2016-02-07 13:25:44

标签: php arrays wordpress

我正在使用以下过滤器为url设置一些参数,其名称='srtby_homepage'的参数。

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

我正在尝试使用一系列值代替下面的这些,但认为iv在某处出错了

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

以下是我目前正在做的事情,2个函数适用于不同的$ runorders:

    $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;
    };

2 个答案:

答案 0 :(得分: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请求,则为OR!您可以通过,的{​​{1}}由implode(',',$array);发送explode(',',$string);with<>

答案 1 :(得分:0)

首先使用key to value创建一个包含options数组的查询字符串,然后将此字符串与您的URL连接

<?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;
?>