この関数の現在の目的は、複数のカンマ区切りの郵便番号リスト (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"); ?>
しますが、これはハックだと確信しています; 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(); ?>
以下の within 関数の結果を参照してください。var_dump($post);
文字制限のため、ごく一部のみを含めました。
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) "テキサス州ミズーリ市" ["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"]=>文字列(72) "["pinged"]=> 文字列(0) "" ["post_modified"]=> 文字列(19) "2018-09-21 15:37:36" ["post_modified_gmt"]=> 文字列(19) "2018- 09-21 15:37:36" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(72) "["pinged"]=> 文字列(0) "" ["post_modified"]=> 文字列(19) "2018-09-21 15:37:36" ["post_modified_gmt"]=> 文字列(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) "テキサス州ヒューストン" ["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) "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) "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"]=> 文字列(0) "" ["post_modified"]=> 文字列(19) "2018-10-10 19:29:31" ["post_modified_gmt"]=> 文字列(19 ) "2018-10-10 19:29:31" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(72) "" ["post_modified"]=> 文字列(19) "2018-10-10 19:29:31" ["post_modified_gmt"]=> 文字列(19) "2018-10-10 19:29:31" ["post_content_filtered "]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(72) "" ["post_modified"]=> 文字列(19) "2018-10-10 19:29:31" ["post_modified_gmt"]=> 文字列(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) "グレープバイン、テキサス州" ["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)」pinged"]=> 文字列(0) "" ["post_modified"]=> 文字列(19) "2018-09-21 15:27:46" ["post_modified_gmt"]=> 文字列(19) "2018-09- 21 15:27:46" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(72) "pinged"]=> 文字列(0) "" ["post_modified"]=> 文字列(19) "2018-09-21 15:27:46" ["post_modified_gmt"]=> 文字列(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) "テキサス州サウスレイク" ["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)」pinged"]=> 文字列(0) "" ["post_modified"]=> 文字列(19) "2018-09-21 15:25:31" ["post_modified_gmt"]=> 文字列(19) "2018-09- 21 15:25:31" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(72) "pinged"]=> 文字列(0) "" ["post_modified"]=> 文字列(19) "2018-09-21 15:25:31" ["post_modified_gmt"]=> 文字列(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 "