如何通过CPT获取条款清单?

问题描述 投票:0回答:1

我的网站上有一张带有圆点的地图,我将地图上每个国家的办公室数量本地化。

它是如何工作的基本上我有CPT作为office我创建帖子作为城市名称像纽约,伦敦等。

例如,如果我在美国纽约设有办事处,我将创建该邮件为纽约,自定义类别将为国家/地区名称为美国。此外,在office CPT中,我有自定义字段用于协调地图上的点作为Home_x和Home_Y。

所以下面代码的结果如下:

  • 美国/纽约
  • 美国/芝加哥
  • 英国/伦敦
  • 英国/布里斯托尔
  • 西班牙/巴塞罗那
  • 西班牙/格拉纳达

我的循环代码是;

    <div class="map-wrapper">
        <div class="map">

            <?php

            $terms = get_terms(array(
                'taxonomy' => 'office-country',
                'hide_empty' => false,
            ));
            ?>
            <?php foreach ($terms as $term) : ?>
                <?php
                $re = explode('-', $term->name);
                $args = array (
                    'post_type' => 'office', //
                    'posts_per_page' => -1,
                    'order' => 'ASC',
                    'tax_query'     => array(
                        array(
                            'taxonomy'  => 'office-country',
                            'field'     => 'id',
                            'terms'     => $term->term_id,
                        ),
                    ),
                );
                $query = new WP_Query( $args );
                if( $query->have_posts() ){
                    while( $query->have_posts() ){
                        $query->the_post();
                        $title =  get_the_title();
                        $info = get_post_meta(get_the_ID(), '_post_info', true);
                        $link = get_term_meta($term->term_id, 'link', true);
                        ?>
                        <a href="<?php echo $link ?>"
                           class="point <?php if ($term->slug === $_GET['country']) echo 'active' ?>"
                           style="left: <?php echo  $info['home_x']; ?>px; top: <?php echo $info['home_y']; ?>px;"
                           data-target=".country-popup-<?php echo $term->term_id ?>">
                            <div class="inner"></div>
                            <div class="text">
                                <span class="name"><?php echo $re[0] ?> </span>
                                    <span class="number"><?php echo " / ".$title; ?>
                                </span>
                            </div>
                        </a>

                        <?php
                    }
                }
            endforeach; ?>

        </div>
    </div>

我的CPT定制领域是;

  <tr>
        <th>
            <label><?php _e('Home X'); ?></label>
        </th>
        <td>
            <input type="text" name="_post_info[home_x]" value="<?php echo $info['home_x'] ?>">
        </td>
    </tr>

    <tr>
        <th>
            <label><?php _e('Home Y'); ?></label>
        </th>
        <td>
            <input type="text" name="_post_info[home_y]" value="<?php echo $info['home_y'] ?>">
        </td>
    </tr>

以上代码完美地用于此目的。

但我想将自定义分类法更改为办公室,并将其作为国家名称发布。而不是为城市创建多个帖子,创建国家帖子和添加城市作为自定义分类更容易。所以我试图改变下面的代码以获得新的方式。

我已经更改了循环代码,如下所示,并为自定义分类创建自定义字段,我也在下面指出它。

新循环;

<div class="map-wrapper">
                <div class="map">

                    <?php

                    $terms = get_terms(array(
                        'taxonomy' => 'office-city',
                        'hide_empty' => false,
                    ));
                    ?>
                    <?php foreach ($terms as $term) : ?>
                        <?php
                        $re = explode('-', $term->name);
                        $args = array (
                            'post_type' => 'office-country', //
                            'posts_per_page' => -1,
                            'order' => 'ASC',
                            'tax_query'     => array(
                                array(
                                    'taxonomy'  => 'office-city',
                                    'field'     => 'id',
                                    'terms'     => $term->term_id,
                                ),
                            ),
                        );

                        $query = new WP_Query( $args );
                        if( $query->have_posts() ){
                            while( $query->have_posts() ){
                                $query->the_post();
                                $title =  get_the_title();
                                // $info = get_post_meta(get_the_ID(), '_post_info', true);
                                $link = get_term_meta($term->term_id, 'link', true);
                                $MapY = get_term_meta($term->term_id, 'home_y', true);
                                $MapX = get_term_meta($term->term_id, 'home_x', true);
                                ?>
                                <a href="<?php echo $link ?>"
                                   class="point <?php if ($term->slug === $_GET['country']) echo 'active' ?>"
                                   style="left: <?php echo  $MapY ?>px; top: <?php echo $MapX ?>px;"
                                   data-target=".country-popup-<?php echo $term->term_id ?>">
                                    <div class="inner"></div>
                                    <div class="text">
                                        <span class="name"><?php echo $title; ?> </span>
                                            <span class="number"><?php echo " / ".$re[0] ?>
                                        </span>
                                    </div>
                                </a>

                                <?php
                            }
                        }
                    endforeach; ?>

                </div>
            </div>

分类的自定义字段;

add_action( 'office-country_edit_form_fields', 'office_country_taxonomy_custom_fields', 10, 2 );
function office_country_taxonomy_custom_fields($tag) {
    ?>

    <tr>
        <th>
            <label><?php _e('Home X'); ?></label>
        </th>
        <td>
            <input type="text" name="_term_meta[home_x]" value="<?php echo get_term_meta($tag->term_id, 'home_x', true)  ?>">
        </td>
    </tr>

    <tr>
        <th>
            <label><?php _e('Home Y'); ?></label>
        </th>
        <td>
            <input type="text" name="_term_meta[home_y]" value="<?php echo get_term_meta($tag->term_id, 'home_y', true) ?>">
        </td>
    </tr>


    <?php
}

但是,当我应用此代码时,它部分工作,我不知道更改查询。

结果;

  • 美国/纽约
  • 英国/伦敦
  • 西班牙/巴塞罗那

因此,它只显示每个分类的帖子,但我想显示每个国家/地区的所有城市,如下所示;

  • 美国/纽约
  • 美国/芝加哥
  • 英国/伦敦
  • 英国/布里斯托尔
  • 西班牙/巴塞罗那
  • 西班牙/格拉纳达

对不起,这是一个很长的问题。我希望你们能帮忙解决这个问题,我希望你们能理解我的英语。

php wordpress custom-taxonomy
1个回答
2
投票

你的循环工作正常,因为我得到你想要的结果。

<?php

$terms = get_terms(array(
    'taxonomy' => 'office-city',
    'hide_empty' => false,
));

foreach ($terms as $term) : 
    $args = array (
        'post_type' => 'office-country', //
        'posts_per_page' => -1,
        'order' => 'ASC',
        'orderby' => 'title',
        'tax_query'     => array(
            array(
                'taxonomy'  => 'office-city',
                'field'     => 'id',
                'terms'     => $term->term_id,
            ),
        ),
    );

    $query = new WP_Query( $args );
    if( $query->have_posts() ){
        while( $query->have_posts() ){
            $query->the_post();
            $title =  get_the_title();
            echo '<p>'. $title . '/' . $term->name . '</p>';
        }
    }
endforeach; ?>
© www.soinside.com 2019 - 2024. All rights reserved.