2016-06-18 91 views

回答

1

是它仍然實際上,你應該檢查this tutorial,其中一個作者給出了關於搜索功能的定製非常詳細的解釋。

一個用於必要的情況下,最有趣的部分:

// meta_query expects nested arrays even if you only have one query 
$sm_query = new WP_Query(array('post_type' => 'accommodation', 'posts_per_page' => '-1', 'meta_query' => array(array('key' => '_sm_accommodation_city')))); 

// The Loop 
if ($sm_query->have_posts()) { 
    $cities = array(); 
    while ($sm_query->have_posts()) { 
     $sm_query->the_post(); 
     $city = get_post_meta(get_the_ID(), '_sm_accommodation_city', true); 

     // populate an array of all occurrences (non duplicated) 
     if(!in_array($city, $cities)){ 
      $cities[] = $city;  
     } 
    } 
} 
} else{ 
     echo 'No accommodations yet!'; 
     return; 
} 


/* Restore original Post Data */ 
wp_reset_postdata(); 

if(count($cities) == 0){ 
    return; 
} 

asort($cities); 

$select_city = '<select name="city" style="width: 100%">'; 
$select_city .= '<option value="" selected="selected">' . __('Select city', 'smashing_plugin') . '</option>'; 
foreach ($cities as $city) { 
    $select_city .= '<option value="' . $city . '">' . $city . '</option>'; 
} 
$select_city .= '</select>' . "\n"; 

reset($cities); 
+1

哦,人......這工作! – Morgari

相關問題