如何获取带有 CPT 的术语列表?
How to get the list of terms with its CPT?
我的网站上有一张地图,上面有一些点,我在地图图片上将每个国家/地区的办事处数量本地化。
它的工作原理基本上是我将 CPT 作为 office
并创建 posts 作为城市名称,如纽约、伦敦等。
例如,如果我在美国纽约有办公室,我会将 post 创建为纽约,自定义类别将国家/地区名称设为美国。此外,在 office
CPT 中,我有自定义字段用于协调地图上的点,如 Home_x 和 Home_Y。
所以下面代码的结果是这样的:
- USA/New约克
- USA/Chicago
- 曼联Kingdom/London
- 曼联Kingdom/Bristol
- Spain/Barcelona
- Spain/Granada
我的循环代码是;
<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>
以上代码非常适合此目的。
但我想现在将自定义分类法更改为办公室,将 post 更改为国家名称。与其为城市创建多个 post,不如创建一个国家 post 并将城市添加为自定义分类要容易得多。所以我正在尝试以一种新的方式更改下面的代码。
我已经将循环代码更改如下,并为自定义分类创建了一个自定义字段,我也在下面指出。
新循环;
<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
}
但是当我应用这段代码时,它部分起作用了,我不知道要更改查询。
结果;
- USA/New约克
- 曼联Kingdom/London
- Spain/Barcelona
因此,它只显示每个分类 posts,但我想显示每个国家/地区的所有城市,如下所示;
- USA/New约克
- USA/Chicago
- 曼联Kingdom/London
- 曼联Kingdom/Bristol
- Spain/Barcelona
- Spain/Granada
抱歉,问题很长。我希望你们能在这方面提供帮助,我希望你们能理解我蹩脚的英语。
你的循环工作正常,因为我得到了你想要的结果。
<?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; ?>
我的网站上有一张地图,上面有一些点,我在地图图片上将每个国家/地区的办事处数量本地化。
它的工作原理基本上是我将 CPT 作为 office
并创建 posts 作为城市名称,如纽约、伦敦等。
例如,如果我在美国纽约有办公室,我会将 post 创建为纽约,自定义类别将国家/地区名称设为美国。此外,在 office
CPT 中,我有自定义字段用于协调地图上的点,如 Home_x 和 Home_Y。
所以下面代码的结果是这样的:
- USA/New约克
- USA/Chicago
- 曼联Kingdom/London
- 曼联Kingdom/Bristol
- Spain/Barcelona
- Spain/Granada
我的循环代码是;
<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>
以上代码非常适合此目的。
但我想现在将自定义分类法更改为办公室,将 post 更改为国家名称。与其为城市创建多个 post,不如创建一个国家 post 并将城市添加为自定义分类要容易得多。所以我正在尝试以一种新的方式更改下面的代码。
我已经将循环代码更改如下,并为自定义分类创建了一个自定义字段,我也在下面指出。
新循环;
<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
}
但是当我应用这段代码时,它部分起作用了,我不知道要更改查询。
结果;
- USA/New约克
- 曼联Kingdom/London
- Spain/Barcelona
因此,它只显示每个分类 posts,但我想显示每个国家/地区的所有城市,如下所示;
- USA/New约克
- USA/Chicago
- 曼联Kingdom/London
- 曼联Kingdom/Bristol
- Spain/Barcelona
- Spain/Granada
抱歉,问题很长。我希望你们能在这方面提供帮助,我希望你们能理解我蹩脚的英语。
你的循环工作正常,因为我得到了你想要的结果。
<?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; ?>