2016-12-21 9 views
2

私はこのコードを使用して、クエリー文字列が存在するかどうかをチェックし、値に基づいて内容をエコーし​​ます。URLにクエリーストリングが表示されていないことを確認

コードがあれば働いている:

www.mywebsite.com/?position=&category=&country= 
www.mywebsite.com/?position=position1&category=&country= 
www.mywebsite.com/?position=&category=category1&country= 
www.mywebsite.com/?position=&category=&country=country1 
www.mywebsite.com/?position=&category=category1&country=country 
. 
. 
and so forth. 

が、URLは、他のブロックが実行されていない親のクエリ文字列せずにただwww.mywebsite.comある場合。

私の現在のコードは次のとおりです。

<?php 
// args 
    if ($_GET){ 
     if ($_GET['category'] && $_GET['country']) { 
      $args = array(
      'numberposts' => -1, 
      'post_type'  => 'job_order', 
      'meta_query' => array(
       'relation'  => 'AND', 
       array(
        'key'  => 'j_category', 
        'value'  => $_GET['category'], 
        'compare' => '=' 
       ), 
       array(
        'key'  => 'j_country', 
        'value'  => $_GET['country'], 
        'compare' => '=' 
        ) 
       ) 
      ); 
     } elseif ($_GET['category']) { 
      $args = array(
       'numberposts' => -1, 
       'post_type'  => 'job_order', 
       'meta_key'  => 'j_category', 
       'meta_value' => $_GET['category'] 
       ); 
     } elseif ($_GET['country']) { 
      $args = array(
       'numberposts' => -1, 
       'post_type'  => 'job_order', 
       'meta_key'  => 'j_country', 
       'meta_value' => $_GET['country'] 
      ); 
     } else { 
      $args = array(
       'numberposts' => -1, 
       'post_type'  => 'job_order' 
       ); 
     } 
    } else{ 
     $args = array(
      'numberposts' => -1, 
      'post_type'  => 'job_order' 
      ); 
    } 
    // query 
    $the_query = new WP_Query($args); 
    if($the_query->have_posts()): 
    while($the_query->have_posts()) : $the_query->the_post(); 
?> 
+0

あなたはあなたのコードの中でポジションを探しているわけではないので、質問には関係ありません。 –

答えて

0

はこれを試してください:あなたは、ネストされた条件文を過ぎて取得されていません

if ($_GET){ 
     if (isset($_GET['category']) && isset($_GET['country'])) { 
      $args = array(
      'numberposts' => -1, 
      'post_type'  => 'job_order', 
      'meta_query' => array(
       'relation'  => 'AND', 
       array(
        'key'  => 'j_category', 
        'value'  => $_GET['category'], 
        'compare' => '=' 
       ), 
       array(
        'key'  => 'j_country', 
        'value'  => $_GET['country'], 
        'compare' => '=' 
        ) 
       ) 
      ); 
     } elseif (isset($_GET['category'])) { 
      $args = array(
       'numberposts' => -1, 
       'post_type'  => 'job_order', 
       'meta_key'  => 'j_category', 
       'meta_value' => $_GET['category'] 
       ); 
     } elseif (isset($_GET['country'])) { 
      $args = array(
       'numberposts' => -1, 
       'post_type'  => 'job_order', 
       'meta_key'  => 'j_country', 
       'meta_value' => $_GET['country'] 
      ); 
     } 
    } else{ 
     $args = array(
      'numberposts' => -1, 
      'post_type'  => 'job_order' 
      ); 
    } 

。最後のelseは、クエリ文字列がないとすぐに実行されます。

そして値を取得する前に値が設定されているかどうかを確認する必要があります。

関連する問題