我试图根据许多条件将不同的值存储到数组中:
<?php $sort= $_GET['sort'];
if($sort == "title") { $args = array('orderby'=>'title','order'=>'ASC'); }
elseif($sort == "date") { $args = array('orderby'=>'date'); }
else{ $args = array('orderby'=>'date','order'=>'DESC'); }
?>
然后我尝试使用WP_Query将变量$args
插入到wordpress循环中,如下所示:
<?php $loop = new WP_Query( array( $args, 'post_type' => 'films', 'post_parent' => 0, 'posts_per_page' => -1 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
wordpress loop stuff, and the end while, end if
这不能正常工作。我是否错误地将数组传递到wordpress循环中?
答案 0 :(得分:0)
将数组$ args传递到另一个数组中。 WP_Query无法理解双嵌套数组。
为什么不根据条件的结果为要分配给orderby和order参数的值设置变量。
if ( $sort == 'title' ) {
$orderby = 'title';
$order = 'ASC';
} esleif ( // ....etc
然后在你的查询数组中:
'orderby' => $orderby, 'order' => $order, etc..