2016-06-18 65 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

相关问题