0

次のようないくつかの条件に基づいて、さまざまな値を配列に格納しようとしています。

<?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'); } 
?>

そして、次$argsのように WP_Query を使用して 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 ループに間違って渡していますか?

4

1 に答える 1

0

配列 $args を別の配列に渡します。WP_Query は、二重にネストされた配列を理解できません。

条件の結果に基づいて、orderby パラメーターと order パラメーターに割り当てたい値の変数を設定してみませんか。

if ( $sort == 'title' ) {
   $orderby = 'title';
   $order = 'ASC';
} esleif ( // ....etc

次に、クエリ配列で:

  'orderby' => $orderby, 'order' => $order, etc..
于 2012-06-23T16:00:30.003 に答える