首页 > 解决方案 > 按州列出 -> 类别 -> 城市

问题描述

我正在尝试获得以下信息:

状态

-医疗保健(类别)

- 城市

---属性 1

---属性 2

我的查询正在努力获得:

状态

-医疗保健(类别)

--属性 1

--属性 2

而且我不知道如何将 City 作为副标题!

这是我的代码:

  <h1 style="padding-bottom: 30px; font-weight: 600;">South Carolina</h1>
 <?php //start by fetching the terms for the animal_cat taxonomy
 $terms = get_terms( 'property-category', array(
  'orderby'    => 'count',
'hide_empty' => 0
 ) );

 // now run a query for each animal family
 foreach( $terms as $term ) {

// Define the query
$args = array(
    'post_type' => 'property',
    'property-category' => $term->slug,
    'meta_query' => array(
            array  (
            'key' => 'state',
            'value' => 'SC'
            ),
             array(
            'key' => 'available',
            'compare' => '=',
            'value' => '1'
            )
            )
);
$query = null;
    $query = new WP_Query( $args );

   // output the category name in a heading tag
   if( $query->have_posts() ) :
   echo'<div class="collapsible">&nbsp;&nbsp;' . $term->name . ''; echo'&nbsp;&nbsp;<span style="font-size:12px; vertical-align: middle;">(' . $query->post_count . ')</span></div>';
    // output the post titles in a list
    echo '<ul class="content">';
        // Start the Loop
        while ( $query->have_posts() ) : $query->the_post(); ?>
        <li style="font-size: 12px; list-style: none; text-transform: none; line-height: 30px; padding-left: 15px;" id="post-<?php the_ID(); ?>">
            <?php the_field('town'); ?>

            // ****THIS IS WHERE I NEED TO SHOW PROPERTIES UNDER CITY???

            <ul>
                <li style="font-size: 12px; list-style: none; text-transform: none; line-height: 30px; padding-left: 15px;" id="post-<?php the_ID(); ?>">
                    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                </li>
            </ul>
        </li>

<?php
endwhile;
endif;
echo '</ul>';

    // use reset postdata to restore orginal query
    wp_reset_postdata();

 } ?>

我完全被卡住了,我需要设置另一个查询吗?

编辑

我现在有了这个,它显示了该城市/城镇中每个可用属性列表上方的城市/城镇,但它复制了这样的城市/城镇标题:

医疗保健

索尔兹伯里

--医务室A

夏洛特

--医务室D

索尔兹伯里

--医务室B

夏洛特

--医务室C

这就是我希望它显示的方式:

医疗保健

索尔兹伯里

--医务室A

--医务室B

夏洛特

--医务室D

--医务室C

这是我的代码:

  <div id="sidebar1">
        <h1 style="padding-bottom: 30px; font-weight: 600; padding-left: 50px;">North Carolina</h1>
  <?php //start by fetching the terms for the animal_cat taxonomy
  $terms = get_terms( 'property-category', array(
  'orderby'    => 'count',
  'hide_empty' => 0
  ) );

  // now run a query for each animal family
  foreach( $terms as $term ) {

// Define the query
$args = array(
    'post_type' => 'property',
    'property-category' => $term->slug,
    'meta_query' => array(
            array  (
            'key' => 'state',
            'value' => 'NC'
            ),
             array(
            'key' => 'available',
            'compare' => '=',
            'value' => '1'
            )
            )
);
$query = null;
$query = new WP_Query( $args );
  // output the category name in a heading tag
  if( $query->have_posts() ) :
   echo'<div class="collapsible G">&nbsp;&nbsp;' . $term->name . '';    echo'&nbsp;&nbsp;<span style="font-size:12px; vertical-align: middle;">(' . $query->post_count . ')</span></div>';
    // output the post titles in a list
    echo '<ul class="content">';
        // Start the Loop
        while ( $query->have_posts() ) : $query->the_post(); ?><?php endwhile;
            echo '<ul style="margin-left: 0;">';
            // Start second Loop
            $args = array(
            'post_type'     => 'property',
            'meta_query' => array  (
            array  (
                        'key' => 'state',
                        'value' => 'NC',
                        ),
                        array(
                        'key' => 'town',
                        )
                        )
       );
            while ( $query->have_posts() ) : $query->the_post(); ?>
        <div><?php the_field('town'); ?></div>
        <li class="city" id="post-<?php the_ID(); ?>">



                <li  style="font-size: 15px; list-style: none; text-transform: none; line-height: 30px;" id="post-<?php the_ID(); ?>">
                    <a class="prop" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>&nbsp;&nbsp;<i class="fas fa-angle-right"></i>
                </li>


    <?php
     endwhile;
    endif;
echo '</ul></li></ul>';

            // use reset postdata to restore orginal query
            wp_reset_postdata();

 } ?>
    </div>

我究竟做错了什么?

标签: phpwordpressadvanced-custom-fields

解决方案


推荐阅读