当自定义类型 post 与另一个自定义类型 post 有连接时,如何获取该数据?
How to get a custom type post data when it has a connection with another custom type post?
我有两个自定义 post 类型:国家和城市。
国家/地区
只有一个文本字段可以填写它的名称。
城市
我有一个城市名称的文本字段,还有一个 select 包含在其他自定义 post 类型中添加的所有国家/地区。
我的问题是,如何获取每个城市、自己的国家?
检索我正在使用的城市:
$args = array (
'post_type' => 'cities'
);
$query = new WP_Query($args);
在这个城市的结果中,我在管理区域中 select 没有任何与国家相关的信息。
谢谢!
如果您使用的是高级自定义字段,请试试这个-
<?php
$args = array (
'post_type' => 'cities'
);
$query = new WP_Query($args);
if ( $query->have_posts() ):
while ( $query->have_posts() ): $query->the_post();
the_title();
$country = get_field('countries', get_the_ID());
echo $country->post_title;
endwhile;
endif;
?>
如果城市和国家只是单一字段内容文本类型:
在 post 类型国家中,创建 country_cat 的自定义分类,然后将城市设为 country_cat.
的子类别
您可以像这样获取子类别的父类别 post 显示:https://wordpress.stackexchange.com/questions/11267/check-is-category-parent-or-not-from-its-id
我有两个自定义 post 类型:国家和城市。
国家/地区
只有一个文本字段可以填写它的名称。
城市
我有一个城市名称的文本字段,还有一个 select 包含在其他自定义 post 类型中添加的所有国家/地区。
我的问题是,如何获取每个城市、自己的国家?
检索我正在使用的城市:
$args = array (
'post_type' => 'cities'
);
$query = new WP_Query($args);
在这个城市的结果中,我在管理区域中 select 没有任何与国家相关的信息。
谢谢!
如果您使用的是高级自定义字段,请试试这个-
<?php
$args = array (
'post_type' => 'cities'
);
$query = new WP_Query($args);
if ( $query->have_posts() ):
while ( $query->have_posts() ): $query->the_post();
the_title();
$country = get_field('countries', get_the_ID());
echo $country->post_title;
endwhile;
endif;
?>
如果城市和国家只是单一字段内容文本类型: 在 post 类型国家中,创建 country_cat 的自定义分类,然后将城市设为 country_cat.
的子类别您可以像这样获取子类别的父类别 post 显示:https://wordpress.stackexchange.com/questions/11267/check-is-category-parent-or-not-from-its-id