我使用高级自定义字段根据状态添加存储位置列表。我希望每个商店属于它的状态填充下的状态的div。我也使用ACF为用户添加一个新的状态div。我似乎无法得到代码拉在商店的位置。有人能帮忙吗?
' ' '
<?php
$args = array(
'post_type' => 'state',
'meta_key' => 'state_name',
'orderby' => 'meta_value',
'order' => 'ASC'
);
$the_query = new WP_Query( $args );
?>
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="state-name-header">
<h2 id="<?php the_field( 'state_name' ); ?>"><?php the_field( 'state_name' ); ?></h2>
<?php
$state = get_field('state_name');
$state_array = $state[0];
$state_ID = $state_array->ID;
$stores = get_posts(array(
'post_type' => 'stores',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'state_name',
'value' => '"' . $state_ID . '"',
'compare' => 'LIKE'
)
)
));
?>
<?php if ( $stores ): ?>
<div class="locations-container">
<?php foreach( $stores as $store ): ?>
<?php
$store_name = get_field( 'store_name' , $store->ID );
$store_address = get_field( 'street_address' , $store->ID );
$store_city = get_field( 'city' , $store->ID );
$store_state_abbr = get_field( 'state_abbreviation' , $store->ID);
$store_zip = get_field( 'zip_code' , $store->ID);
$store_phone = get_field( 'phone' , $store->ID );
$store_url = get_field( 'website' , $store->ID );
?>
<div class="store-location">
<h4 class="location-name"><?php echo $store_name; ?></h4>
<p><?php echo $store_address ?></p>
<p><?php echo $store_city ?></p>
<p><?php echo $store_state_abbr ?>, <?php echo $store_zip ?></p>
<p><?php echo $store_phone ?></p>
<a href="<?php echo $store_url ?>">website</a>
</div>
<?php endforeach; ?>
</div><!-- end locations-conatiner-->
<?php endif; ?>
</div><!-- end state-name-header -->
<?php endwhile; endif; ?>
</div><!-- end state-listings-container -->```
我重新构造了Advanced Custom Fields以使用一个中继器,而不是为州和位置提供单独的条目。这非常有效。这是我调整查询的工作代码。
' ' '
<?php
$args = array(
'post_type' => 'location',
'meta_key' => 'state_name',
'orderby' => 'meta_value',
'order' => 'ASC'
);
$the_query = new WP_Query( $args );
?>
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="state-name-header">
<h2 id="<?php the_field( 'state_name' ); ?>"><?php the_field( 'state_name' ); ?></h2>
<div class="locations-container">
<?php if(get_field('new_store')): ?>
<?php while(has_sub_field('new_store')): ?>
<div class="store-location">
<h4 class="location-name"><?php the_sub_field('store_name'); ?></h4>
<p><?php the_sub_field('street_address'); ?></p>
<p><?php the_sub_field('city'); ?></p>
<p><?php the_field('state_abbreviation'); ?>, <?php the_sub_field('zip_code'); ?></p>
<p><?php the_sub_field('phone'); ?></p>
<a href="<?php the_sub_field('website'); ?>">Website</a>
</div><!-- end store-location -->
<?php endwhile; ?>
<?php endif; ?>
</div><!-- end locations-container -->
</div><!-- end state-name-header -->
<?php endwhile; endif; ?>
</div><!-- end state-listings-container -->```