我正在编写一个 PHP 函数来搜索 Wordpress 模板文件中的一组邮政编码数组。为什么我的函数 return 没有值?
I'm writing a PHP function to search through array an array of zip codes in a Wordpress template file. Why won't my function return a value?
该函数现在的目的是遍历多个逗号分隔的邮政编码列表(zip_codes_serviced 的高级自定义字段)并将其与特定的邮政编码(33606 用于测试目的)匹配。如果匹配,该函数应打印与该邮政编码关联的城市和州字段。在这种情况下,佛罗里达州坦帕市。见下面的功能:
function zip_search($userZip){
$posts = get_posts(array(
'posts_per_page' => -1,
'post_type' => 'Location'
));
if( $posts ):
foreach( $posts as $post ):
$zipField=get_field('zip_codes_serviced');
//echo $zipField;
$zipString = $zipField . ', ';
//echo $zipArray;
$array = explode(', ' , $zipString); //split string into array seperated by ', '
foreach($array as $value) //loop over values
{
$cityField=get_field('city');
$stateField=get_field('state');
if($value==$userZip){
return ($cityField . '<br>' . $stateField);
}
}
endforeach;
wp_reset_postdata();
endif;
}
然后我这样调用函数:
zip_search(33606);
我也这样试过:
function zip_search(){
$posts = get_posts(array(
'posts_per_page' => -1,
'post_type' => 'Location'
));
//Set user zip to 33606 for testing purposes
$userZip=33606;
if( $posts ):
foreach( $posts as $post ):
$zipField=get_field('zip_codes_serviced');
//echo $zipField;
$zipString = $zipField . ', ';
//echo $zipArray;
$array = explode(', ' , $zipString); //split string into array seperated by ', '
foreach($array as $value) //loop over values
{
$cityField=get_field('city');
$stateField=get_field('state');
//echo $value. '<br>';
if($value==$userZip){
echo ($cityField . '<br>' . $stateField); //print
}
}
endforeach;
wp_reset_postdata();
endif;
}
然后这样调用:
zip_search();
这不应该在页面上打印出城市和州字段吗?当我将逻辑放在函数之外时,它可以工作,但我无法让它在函数内工作。有人有什么建议吗?
我让它工作的唯一方法是完全在一个函数之外——创建一个文件 zip-search.php
,然后在我想要它显示的页面上插入 <?php include("phone-display.php"); ?>
,我很漂亮确定这是一个 hack; zip-search.php
如下所示:
$posts = get_posts(array(
'posts_per_page' => -1,
'post_type' => 'Location'
));
//Set user zip to 33606 for testing purposes
$userZip=33606;
if( $posts ):
foreach( $posts as $post ):
$zipField=get_field('zip_codes_serviced');
//echo $zipField;
$zipString = $zipField . ', ';
//echo $zipArray;
$array = explode(', ' , $zipString); //split string into array seperated by ', '
foreach($array as $value) //loop over values
{
$cityField=get_field('city');
$stateField=get_field('state');
//echo $value. '<br>';
if($value==$userZip){
echo ($cityField . '<br>' . $stateField); //print
}
}
endforeach;
wp_reset_postdata();
endif;
我试图让邮政编码功能在其中工作的整个模板文件如下所示:
<?php
/*
Template Name: Location
*
*/
get_header();
nectar_page_header($post->ID);
//full page
$fp_options = nectar_get_full_page_options();
extract($fp_options);
?>
<?php
function zip_search($userZip){
$posts = get_posts(array(
'posts_per_page' => -1,
'post_type' => 'Location'
));
//Set user zip to 33606 for testing purposes
//$userZip=33606;
if( $posts ):
foreach( $posts as $post ):
$zipField=get_field('zip_codes_serviced');
//echo $zipField;
$zipString = $zipField . ', ';
//echo $zipArray;
$array = explode(', ' , $zipString); //split string into array seperated by ', '
foreach($array as $value) //loop over values
{
$cityField=get_field('city');
$stateField=get_field('state');
//echo $value. '<br>';
if($value==$userZip){
echo ($cityField . '<br>' . $stateField); //print
}
}
endforeach;
wp_reset_postdata();
endif;
}
?>
<div class="container-wrap">
<div class="<?php if($page_full_screen_rows != 'on') echo 'container'; ?> main-content">
<div class="row">
<?php
//breadcrumbs
if ( function_exists( 'yoast_breadcrumb' ) && !is_home() && !is_front_page() ){ yoast_breadcrumb('<p id="breadcrumbs">','</p>'); }
//buddypress
global $bp;
if($bp && !bp_is_blog_page()) echo '<h1>' . get_the_title() . '</h1>';
//fullscreen rows
if($page_full_screen_rows == 'on') echo '<div id="nectar_fullscreen_rows" data-animation="'.$page_full_screen_rows_animation.'" data-row-bg-animation="'.$page_full_screen_rows_bg_img_animation.'" data-animation-speed="'.$page_full_screen_rows_animation_speed.'" data-content-overflow="'.$page_full_screen_rows_content_overflow.'" data-mobile-disable="'.$page_full_screen_rows_mobile_disable.'" data-dot-navigation="'.$page_full_screen_rows_dot_navigation.'" data-footer="'.$page_full_screen_rows_footer.'" data-anchors="'.$page_full_screen_rows_anchors.'">';
if(have_posts()) : while(have_posts()) : the_post();
the_content();
endwhile; endif;
if($page_full_screen_rows == 'on') echo '</div>'; ?>
</div><!--/row-->
</div><!--/container-->
<span><?php zip_search(33606);?></span>
<span>Locations Landing Page--where zip code function can go </span>
</div><!--/container-wrap-->
<?php get_footer(); ?>
请参阅下面 var_dump($post);
within 函数的结果,由于字符限制,我只包含了一小部分:
array(37) { [0]=> object(WP_Post)#1683 (24) { ["ID"]=> int(1490)
["post_author"]=> string(1) "1" ["post_date"]=> string(19) "2018-09-21
15:39:29" ["post_date_gmt"]=> string(19) "2018-09-21 15:39:29"
["post_content"]=> string(0) "" ["post_title"]=> string(14)
"Scottsdale, AZ" ["post_excerpt"]=> string(0) "" ["post_status"]=>
string(7) "publish" ["comment_status"]=> string(6) "closed"
["ping_status"]=> string(6) "closed" ["post_password"]=> string(0) ""
["post_name"]=> string(13) "scottsdale-az" ["to_ping"]=> string(0) ""
["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2018-09-21
20:54:47" ["post_modified_gmt"]=> string(19) "2018-09-21 20:54:47"
["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0)
["guid"]=> string(72)
"http://dev-site-name.hostingsite.my/?post_type=location&p=1490"
["menu_order"]=> int(0) ["post_type"]=> string(8) "location"
["post_mime_type"]=> string(0) "" ["comment_count"]=> string(1) "0"
["filter"]=> string(3) "raw" } [1]=> object(WP_Post)#1684 (24) {
["ID"]=> int(1488) ["post_author"]=> string(1) "1" ["post_date"]=>
string(19) "2018-09-21 15:37:36" ["post_date_gmt"]=> string(19)
"2018-09-21 15:37:36" ["post_content"]=> string(0) "" ["post_title"]=>
string(17) "Missouri City, TX" ["post_excerpt"]=> string(0) ""
["post_status"]=> string(7) "publish" ["comment_status"]=> string(6)
"closed" ["ping_status"]=> string(6) "closed" ["post_password"]=>
string(0) "" ["post_name"]=> string(16) "missouri-city-tx"
["to_ping"]=> string(0) "" ["pinged"]=> string(0) ""
["post_modified"]=> string(19) "2018-09-21 15:37:36"
["post_modified_gmt"]=> string(19) "2018-09-21 15:37:36"
["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0)
["guid"]=> string(72)
"http://dev-site-name.hostingsite.my/?post_type=location&p=1488"
["menu_order"]=> int(0) ["post_type"]=> string(8) "location"
["post_mime_type"]=> string(0) "" ["comment_count"]=> string(1) "0"
["filter"]=> string(3) "raw" } [2]=> object(WP_Post)#1685 (24) {
["ID"]=> int(1486) ["post_author"]=> string(1) "1" ["post_date"]=>
string(19) "2018-09-21 15:35:35" ["post_date_gmt"]=> string(19)
"2018-09-21 15:35:35" ["post_content"]=> string(0) "" ["post_title"]=>
string(11) "Houston, TX" ["post_excerpt"]=> string(0) ""
["post_status"]=> string(7) "publish" ["comment_status"]=> string(6)
"closed" ["ping_status"]=> string(6) "closed" ["post_password"]=>
string(0) "" ["post_name"]=> string(10) "houston-tx" ["to_ping"]=>
string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19)
"2018-09-21 15:35:35" ["post_modified_gmt"]=> string(19) "2018-09-21
15:35:35" ["post_content_filtered"]=> string(0) "" ["post_parent"]=>
int(0) ["guid"]=> string(72)
"http://dev-site-name.hostingsite.my/?post_type=location&p=1486"
["menu_order"]=> int(0) ["post_type"]=> string(8) "location"
["post_mime_type"]=> string(0) "" ["comment_count"]=> string(1) "0"
["filter"]=> string(3) "raw" } [3]=> object(WP_Post)#1686 (24) {
["ID"]=> int(1484) ["post_author"]=> string(1) "1" ["post_date"]=>
string(19) "2018-09-21 15:29:24" ["post_date_gmt"]=> string(19)
"2018-09-21 15:29:24" ["post_content"]=> string(0) "" ["post_title"]=>
string(9) "Sugarland" ["post_excerpt"]=> string(0) ""
["post_status"]=> string(7) "publish" ["comment_status"]=> string(6)
"closed" ["ping_status"]=> string(6) "closed" ["post_password"]=>
string(0) "" ["post_name"]=> string(9) "sugarland" ["to_ping"]=>
string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19)
"2018-10-10 19:29:31" ["post_modified_gmt"]=> string(19) "2018-10-10
19:29:31" ["post_content_filtered"]=> string(0) "" ["post_parent"]=>
int(0) ["guid"]=> string(72)
"http://dev-site-name.hostingsite.my/?post_type=location&p=1484"
["menu_order"]=> int(0) ["post_type"]=> string(8) "location"
["post_mime_type"]=> string(0) "" ["comment_count"]=> string(1) "0"
["filter"]=> string(3) "raw" } [4]=> object(WP_Post)#1687 (24) {
["ID"]=> int(1482) ["post_author"]=> string(1) "1" ["post_date"]=>
string(19) "2018-09-21 15:27:46" ["post_date_gmt"]=> string(19)
"2018-09-21 15:27:46" ["post_content"]=> string(0) "" ["post_title"]=>
string(13) "Grapevine, TX" ["post_excerpt"]=> string(0) ""
["post_status"]=> string(7) "publish" ["comment_status"]=> string(6)
"closed" ["ping_status"]=> string(6) "closed" ["post_password"]=>
string(0) "" ["post_name"]=> string(12) "grapevine-tx" ["to_ping"]=>
string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19)
"2018-09-21 15:27:46" ["post_modified_gmt"]=> string(19) "2018-09-21
15:27:46" ["post_content_filtered"]=> string(0) "" ["post_parent"]=>
int(0) ["guid"]=> string(72)
"http://dev-site-name.hostingsite.my/?post_type=location&p=1482"
["menu_order"]=> int(0) ["post_type"]=> string(8) "location"
["post_mime_type"]=> string(0) "" ["comment_count"]=> string(1) "0"
["filter"]=> string(3) "raw" } [5]=> object(WP_Post)#1688 (24) {
["ID"]=> int(1480) ["post_author"]=> string(1) "1" ["post_date"]=>
string(19) "2018-09-21 15:25:31" ["post_date_gmt"]=> string(19)
"2018-09-21 15:25:31" ["post_content"]=> string(0) "" ["post_title"]=>
string(13) "Southlake, TX" ["post_excerpt"]=> string(0) ""
["post_status"]=> string(7) "publish" ["comment_status"]=> string(6)
"closed" ["ping_status"]=> string(6) "closed" ["post_password"]=>
string(0) "" ["post_name"]=> string(12) "southlake-tx" ["to_ping"]=>
string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19)
"2018-09-21 15:25:31" ["post_modified_gmt"]=> string(19) "2018-09-21
15:25:31" ["post_content_filtered"]=> string(0) "" ["post_parent"]=>
int(0) ["guid"]=> string(72)
"http://dev-site-name.hostingsite.my/?post_type=location&p=1480"
我最终这样做了,最后决定 return post 的永久链接比城市和州更好:
function zip_search($userZip){
$args = array(
'posts_per_page' => -1,
'post_type' => 'Locations'
);
$wp_query = new WP_Query($args);
if( $wp_query->have_posts() ): while( $wp_query->have_posts() ) : $wp_query->the_post();
$zipField=get_field('zip_codes_services');
$zipString = $zipField . ', ';
$array = explode(', ' , $zipString); //split string into array seperated by ', '
foreach($array as $value) //loop over values
{
if($value==$userZip){
$post_id = get_the_ID();
$permalink=get_permalink($post_id);
return ($permalink); //print
}
}
endwhile;
wp_reset_postdata();
endif;
}
该函数现在的目的是遍历多个逗号分隔的邮政编码列表(zip_codes_serviced 的高级自定义字段)并将其与特定的邮政编码(33606 用于测试目的)匹配。如果匹配,该函数应打印与该邮政编码关联的城市和州字段。在这种情况下,佛罗里达州坦帕市。见下面的功能:
function zip_search($userZip){
$posts = get_posts(array(
'posts_per_page' => -1,
'post_type' => 'Location'
));
if( $posts ):
foreach( $posts as $post ):
$zipField=get_field('zip_codes_serviced');
//echo $zipField;
$zipString = $zipField . ', ';
//echo $zipArray;
$array = explode(', ' , $zipString); //split string into array seperated by ', '
foreach($array as $value) //loop over values
{
$cityField=get_field('city');
$stateField=get_field('state');
if($value==$userZip){
return ($cityField . '<br>' . $stateField);
}
}
endforeach;
wp_reset_postdata();
endif;
}
然后我这样调用函数:
zip_search(33606);
我也这样试过:
function zip_search(){
$posts = get_posts(array(
'posts_per_page' => -1,
'post_type' => 'Location'
));
//Set user zip to 33606 for testing purposes
$userZip=33606;
if( $posts ):
foreach( $posts as $post ):
$zipField=get_field('zip_codes_serviced');
//echo $zipField;
$zipString = $zipField . ', ';
//echo $zipArray;
$array = explode(', ' , $zipString); //split string into array seperated by ', '
foreach($array as $value) //loop over values
{
$cityField=get_field('city');
$stateField=get_field('state');
//echo $value. '<br>';
if($value==$userZip){
echo ($cityField . '<br>' . $stateField); //print
}
}
endforeach;
wp_reset_postdata();
endif;
}
然后这样调用:
zip_search();
这不应该在页面上打印出城市和州字段吗?当我将逻辑放在函数之外时,它可以工作,但我无法让它在函数内工作。有人有什么建议吗?
我让它工作的唯一方法是完全在一个函数之外——创建一个文件 zip-search.php
,然后在我想要它显示的页面上插入 <?php include("phone-display.php"); ?>
,我很漂亮确定这是一个 hack; zip-search.php
如下所示:
$posts = get_posts(array(
'posts_per_page' => -1,
'post_type' => 'Location'
));
//Set user zip to 33606 for testing purposes
$userZip=33606;
if( $posts ):
foreach( $posts as $post ):
$zipField=get_field('zip_codes_serviced');
//echo $zipField;
$zipString = $zipField . ', ';
//echo $zipArray;
$array = explode(', ' , $zipString); //split string into array seperated by ', '
foreach($array as $value) //loop over values
{
$cityField=get_field('city');
$stateField=get_field('state');
//echo $value. '<br>';
if($value==$userZip){
echo ($cityField . '<br>' . $stateField); //print
}
}
endforeach;
wp_reset_postdata();
endif;
我试图让邮政编码功能在其中工作的整个模板文件如下所示:
<?php
/*
Template Name: Location
*
*/
get_header();
nectar_page_header($post->ID);
//full page
$fp_options = nectar_get_full_page_options();
extract($fp_options);
?>
<?php
function zip_search($userZip){
$posts = get_posts(array(
'posts_per_page' => -1,
'post_type' => 'Location'
));
//Set user zip to 33606 for testing purposes
//$userZip=33606;
if( $posts ):
foreach( $posts as $post ):
$zipField=get_field('zip_codes_serviced');
//echo $zipField;
$zipString = $zipField . ', ';
//echo $zipArray;
$array = explode(', ' , $zipString); //split string into array seperated by ', '
foreach($array as $value) //loop over values
{
$cityField=get_field('city');
$stateField=get_field('state');
//echo $value. '<br>';
if($value==$userZip){
echo ($cityField . '<br>' . $stateField); //print
}
}
endforeach;
wp_reset_postdata();
endif;
}
?>
<div class="container-wrap">
<div class="<?php if($page_full_screen_rows != 'on') echo 'container'; ?> main-content">
<div class="row">
<?php
//breadcrumbs
if ( function_exists( 'yoast_breadcrumb' ) && !is_home() && !is_front_page() ){ yoast_breadcrumb('<p id="breadcrumbs">','</p>'); }
//buddypress
global $bp;
if($bp && !bp_is_blog_page()) echo '<h1>' . get_the_title() . '</h1>';
//fullscreen rows
if($page_full_screen_rows == 'on') echo '<div id="nectar_fullscreen_rows" data-animation="'.$page_full_screen_rows_animation.'" data-row-bg-animation="'.$page_full_screen_rows_bg_img_animation.'" data-animation-speed="'.$page_full_screen_rows_animation_speed.'" data-content-overflow="'.$page_full_screen_rows_content_overflow.'" data-mobile-disable="'.$page_full_screen_rows_mobile_disable.'" data-dot-navigation="'.$page_full_screen_rows_dot_navigation.'" data-footer="'.$page_full_screen_rows_footer.'" data-anchors="'.$page_full_screen_rows_anchors.'">';
if(have_posts()) : while(have_posts()) : the_post();
the_content();
endwhile; endif;
if($page_full_screen_rows == 'on') echo '</div>'; ?>
</div><!--/row-->
</div><!--/container-->
<span><?php zip_search(33606);?></span>
<span>Locations Landing Page--where zip code function can go </span>
</div><!--/container-wrap-->
<?php get_footer(); ?>
请参阅下面 var_dump($post);
within 函数的结果,由于字符限制,我只包含了一小部分:
array(37) { [0]=> object(WP_Post)#1683 (24) { ["ID"]=> int(1490) ["post_author"]=> string(1) "1" ["post_date"]=> string(19) "2018-09-21 15:39:29" ["post_date_gmt"]=> string(19) "2018-09-21 15:39:29" ["post_content"]=> string(0) "" ["post_title"]=> string(14) "Scottsdale, AZ" ["post_excerpt"]=> string(0) "" ["post_status"]=> string(7) "publish" ["comment_status"]=> string(6) "closed" ["ping_status"]=> string(6) "closed" ["post_password"]=> string(0) "" ["post_name"]=> string(13) "scottsdale-az" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2018-09-21 20:54:47" ["post_modified_gmt"]=> string(19) "2018-09-21 20:54:47" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(72) "http://dev-site-name.hostingsite.my/?post_type=location&p=1490" ["menu_order"]=> int(0) ["post_type"]=> string(8) "location" ["post_mime_type"]=> string(0) "" ["comment_count"]=> string(1) "0" ["filter"]=> string(3) "raw" } [1]=> object(WP_Post)#1684 (24) { ["ID"]=> int(1488) ["post_author"]=> string(1) "1" ["post_date"]=> string(19) "2018-09-21 15:37:36" ["post_date_gmt"]=> string(19) "2018-09-21 15:37:36" ["post_content"]=> string(0) "" ["post_title"]=> string(17) "Missouri City, TX" ["post_excerpt"]=> string(0) "" ["post_status"]=> string(7) "publish" ["comment_status"]=> string(6) "closed" ["ping_status"]=> string(6) "closed" ["post_password"]=> string(0) "" ["post_name"]=> string(16) "missouri-city-tx" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2018-09-21 15:37:36" ["post_modified_gmt"]=> string(19) "2018-09-21 15:37:36" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(72) "http://dev-site-name.hostingsite.my/?post_type=location&p=1488" ["menu_order"]=> int(0) ["post_type"]=> string(8) "location" ["post_mime_type"]=> string(0) "" ["comment_count"]=> string(1) "0" ["filter"]=> string(3) "raw" } [2]=> object(WP_Post)#1685 (24) { ["ID"]=> int(1486) ["post_author"]=> string(1) "1" ["post_date"]=> string(19) "2018-09-21 15:35:35" ["post_date_gmt"]=> string(19) "2018-09-21 15:35:35" ["post_content"]=> string(0) "" ["post_title"]=> string(11) "Houston, TX" ["post_excerpt"]=> string(0) "" ["post_status"]=> string(7) "publish" ["comment_status"]=> string(6) "closed" ["ping_status"]=> string(6) "closed" ["post_password"]=> string(0) "" ["post_name"]=> string(10) "houston-tx" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2018-09-21 15:35:35" ["post_modified_gmt"]=> string(19) "2018-09-21 15:35:35" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(72) "http://dev-site-name.hostingsite.my/?post_type=location&p=1486" ["menu_order"]=> int(0) ["post_type"]=> string(8) "location" ["post_mime_type"]=> string(0) "" ["comment_count"]=> string(1) "0" ["filter"]=> string(3) "raw" } [3]=> object(WP_Post)#1686 (24) { ["ID"]=> int(1484) ["post_author"]=> string(1) "1" ["post_date"]=> string(19) "2018-09-21 15:29:24" ["post_date_gmt"]=> string(19) "2018-09-21 15:29:24" ["post_content"]=> string(0) "" ["post_title"]=> string(9) "Sugarland" ["post_excerpt"]=> string(0) "" ["post_status"]=> string(7) "publish" ["comment_status"]=> string(6) "closed" ["ping_status"]=> string(6) "closed" ["post_password"]=> string(0) "" ["post_name"]=> string(9) "sugarland" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2018-10-10 19:29:31" ["post_modified_gmt"]=> string(19) "2018-10-10 19:29:31" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(72) "http://dev-site-name.hostingsite.my/?post_type=location&p=1484" ["menu_order"]=> int(0) ["post_type"]=> string(8) "location" ["post_mime_type"]=> string(0) "" ["comment_count"]=> string(1) "0" ["filter"]=> string(3) "raw" } [4]=> object(WP_Post)#1687 (24) { ["ID"]=> int(1482) ["post_author"]=> string(1) "1" ["post_date"]=> string(19) "2018-09-21 15:27:46" ["post_date_gmt"]=> string(19) "2018-09-21 15:27:46" ["post_content"]=> string(0) "" ["post_title"]=> string(13) "Grapevine, TX" ["post_excerpt"]=> string(0) "" ["post_status"]=> string(7) "publish" ["comment_status"]=> string(6) "closed" ["ping_status"]=> string(6) "closed" ["post_password"]=> string(0) "" ["post_name"]=> string(12) "grapevine-tx" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2018-09-21 15:27:46" ["post_modified_gmt"]=> string(19) "2018-09-21 15:27:46" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(72) "http://dev-site-name.hostingsite.my/?post_type=location&p=1482" ["menu_order"]=> int(0) ["post_type"]=> string(8) "location" ["post_mime_type"]=> string(0) "" ["comment_count"]=> string(1) "0" ["filter"]=> string(3) "raw" } [5]=> object(WP_Post)#1688 (24) { ["ID"]=> int(1480) ["post_author"]=> string(1) "1" ["post_date"]=> string(19) "2018-09-21 15:25:31" ["post_date_gmt"]=> string(19) "2018-09-21 15:25:31" ["post_content"]=> string(0) "" ["post_title"]=> string(13) "Southlake, TX" ["post_excerpt"]=> string(0) "" ["post_status"]=> string(7) "publish" ["comment_status"]=> string(6) "closed" ["ping_status"]=> string(6) "closed" ["post_password"]=> string(0) "" ["post_name"]=> string(12) "southlake-tx" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2018-09-21 15:25:31" ["post_modified_gmt"]=> string(19) "2018-09-21 15:25:31" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(72) "http://dev-site-name.hostingsite.my/?post_type=location&p=1480"
我最终这样做了,最后决定 return post 的永久链接比城市和州更好:
function zip_search($userZip){
$args = array(
'posts_per_page' => -1,
'post_type' => 'Locations'
);
$wp_query = new WP_Query($args);
if( $wp_query->have_posts() ): while( $wp_query->have_posts() ) : $wp_query->the_post();
$zipField=get_field('zip_codes_services');
$zipString = $zipField . ', ';
$array = explode(', ' , $zipString); //split string into array seperated by ', '
foreach($array as $value) //loop over values
{
if($value==$userZip){
$post_id = get_the_ID();
$permalink=get_permalink($post_id);
return ($permalink); //print
}
}
endwhile;
wp_reset_postdata();
endif;
}