我有一个网站可以检测用户的位置,并且只显示具有匹配城市的分类的帖子。如果没有匹配,则将用户重定向到页面以选择可用城市。这是我的功能:
function my_location($q){
if (!$q->is_main_query())
return;
if ($q->is_search())
return;
if ($q->is_archive()){
if ( ! is_admin()) {
if ($userSlug!='Set'){
$userInfo = geoip_detect_get_info_from_current_ip();
switch ($userInfo->postal_code){
case '86403':
case '86404':
case '86405':
case '86406':
$city="lake-havasu-city";
break;
case '86401':
case '86402':
case '86409':
$city="kingman";
break;
case '86429':
case '86430':
case '86439':
case '86442':
$city="bullhead-city";
break;
default:
force_post_city($city);
exit;
}
$q->set( 'tax_query', array(array('taxonomy' => 'pa_city','field' => 'slug',terms' => array( $city ),'operator' => 'IN')));
}}
}
}
add_action( 'pre_get_posts', 'my_location' );
我的问题是,在用户选择城市的页面上,如何将城市传回此功能以便他们拉出相应的城市?这是我的表格:
<form method="post" action="new_location($term_taxonomy)">
<?php
function get_terms_dropdown($taxonomies, $args){
$myterms = get_terms($taxonomies, $args);
$optionname = "optionname";
$emptyvalue = "";
$output ="<select name='".$optionname."'><option selected='".$selected."' value='".$emptyvalue."'>Select a City</option>'";
foreach($myterms as $term){
$term_taxonomy=$term->pa_city; //CHANGE ME
$term_slug=$term->slug;
$term_name =$term->name;
$link = $term_slug;
$output .="<option name='".$link."' value='".$link."'>".$term_name."</option>";
}
$output .="</select>";
return $output;
}
$taxonomies = array('pa_city'); // CHANGE ME
$args = array('order'=>'ASC','hide_empty'=>true);
echo get_terms_dropdown($taxonomies, $args);
?>
<input type="submit" value="click" name="submit">
</form>
非常感谢任何帮助!
答案 0 :(得分:1)
会议建议是关键。我选择了一个cookie。所以,首先我添加了一个函数,用于在wordpress的init上检查cookie。如果它不存在,我们会尝试确定它们的位置并编写cookie。然后在pre_get_post钩子中,我们将它们引导到带有城市交易的页面,或者当城市与现有城市不匹配时,我们将它们引导到搜索城市页面:
add_action( 'init', 'my_setcookie' );
function my_setcookie() {
if(!isset($_COOKIE['city'])) {
$userInfo = geoip_detect_get_info_from_current_ip();
switch ($userInfo->postal_code){
case '86403':
case '86404':
case '86405':
case '86406':
$city="lake-havasu-city";
break;
case '86401':
case '86402':
case '86409':
$city="kingman";
break;
case '86429':
case '86430':
case '86439':
case '86442':
$city="bullhead-city";
break;
}
setcookie( 'city', $city, time() + 3600, COOKIEPATH, COOKIE_DOMAIN );
}}
function my_location( $q ){
if (!$q->is_main_query() )
return;
if ($q->is_search())
return;
if ($q->is_archive() ){
if ( ! is_admin() ) {
if ($userSlug!='Set'){
if (empty($_COOKIE['city'] )) {
echo "<script>window.location.href = 'http://thewebsite.com/select-city/';</script>";
exit();
}
$city = isset( $_COOKIE['city'] ) ? $_COOKIE['city'] : 'not set';
$q->set( 'tax_query', array(array( 'taxonomy' => 'pa_city', 'field' => 'slug', 'terms' => array( $city ), 'operator' => 'IN' )));
}}
}
}
add_action( 'pre_get_posts', 'my_location' );
现在我们将它们放在搜索城市页面中。我们从属性字段中提取可用城市并列出它们,点击后执行标题中的javascript以更新cookie并将其发送到交易:
<form name="myCity" action="http://thewebsite.com/" method="POST">
<?php
function get_terms_dropdown($taxonomies, $args){
$myterms = get_terms($taxonomies, $args);
$optionname = "optionname";
$emptyvalue = "";
$output ="<select name='".$optionname."'><option selected='".$selected."' value='".$emptyvalue."'>Select a City</option>'";
foreach($myterms as $term){
$term_taxonomy=$term->pa_city; //CHANGE ME
$term_slug=$term->slug;
$term_name =$term->name;
$link = $term_slug;
$output .="<option name='".$link."' value='".$link."'>".$term_name."</option>";
}
$output .="</select>";
return $output;
}
$taxonomies = array('pa_city');
$args = array('order'=>'ASC','hide_empty'=>true);
echo get_terms_dropdown($taxonomies, $args);
?>
<input type="submit" value="click" name="submit" onclick="WriteCookie()">
</form>
这是javascript,我们删除现有的cookie并设置新的:
<script type="text/javascript">
function WriteCookie()
{
document.cookie = "city" + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
cookievalue = escape(document.myCity.optionname.value) + ";"
document.cookie='city='+cookievalue +'; expires=Fri, 3 Aug 2021 20:47:11 UTC; path=/';
window.location.href = "http://thewebsite.com"
}
像魅力一样!感谢Halfer的会议建议。