2016-09-13 64 views
2

我正在使用高級自定義字段根據狀態添加商店位置列表。我希望每個屬於它的狀態的商店都在該狀態的div下進行填充。我也使用ACF爲用戶添加一個新的狀態div。我似乎無法讓代碼拉入商店位置。有人能幫忙嗎?高級自定義字段顯示正確狀態下的商店位置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 -->``` 
+0

我很困惑,您的問題。您的查詢沒有任何結果?或者沒有數據正確顯示? – Blake

+0

我沒有任何結果顯示。 –

+0

如果你'var_dump($ stores);'?值是多少? – Blake

回答

1

我重組了高級自定義字段使用中繼器,而不必爲各州和地方獨立的條目。這工作完美。這是我調整後的工作代碼查詢。

```

  <?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 -->``` 
相關問題