我正在尝试为列出菜单项的查询动态分配排序顺序(存储在下面的$ current_sort中)。
如果我对排序顺序进行硬编码,它可以正常工作,但是,当我尝试将排序参数动态分配给字符串时,它会失败。我错过了什么?
$current_sort = ", 'orderby' => 'title', 'order' => 'asc'";
$myposts = get_posts(
array(
'cat' => "$cat,-$catHidden",
'numberposts' => $my_current_count . $current_sort
));
//If I hard code the value of $current_sort it works fine
$myposts = get_posts(
array(
'cat' => "$cat,-$catHidden",
'numberposts' => $my_current_count,
'orderby' => 'title',
'order' => 'asc'));
答案 0 :(得分:1)
您无法将字符串转换为PHP源代码。 (至少你不应该。)
试试这个:
$current_sort_order = "title";
$current_sort_direction = "asc";
$myposts = get_posts(array(
'cat' => "$cat,-$catHidden",
'numberposts' => $my_current_count,
'orderby' => $current_sort_order,
'order' => $current_sort_direction
) );
答案 1 :(得分:0)
你做错了,你没有必须联合......这个:
$ current_sort =“title”; $ order =“asc”; $ myposts = get_posts( 阵列( 'cat'=> “$猫, - $ catHidden” 'numberposts'=> $ my_current_count, '的OrderBy'=> $ current_sort, 'order'=> $秩序 ));
答案 2 :(得分:0)
行上的字符串连接
'numberposts' => $my_current_count . $current_sort
不等同于创建多个数组元素,如
'numberposts' => $my_current_count,
'orderby' => 'title',
'order' => 'asc'));
在第一个实例中,numberposts
成为包含有关排序信息的字符串。
在第二个实例中,numberposts
仅包含当前计数。
更好的选择可能是:
$orderoption="<TITLE HERE>";
$order_dir="<SORT OPTION HERE>";
$myposts = get_posts(
array(
'cat' => "$cat,-$catHidden",
'numberposts' => $my_current_count,
'orderby' => $orderoption,
'order' => $order_dir));