admin管理员组文章数量:1414621
I have a custom post type named 'lawyer' and it contains various custom fields. two of the custom fields are 'state' and 'city'. i want to create a archive page or simple page(anyone will do) which can list all the values of city for specific state. for example i have 10 listings. 5 of which have custom field 'state' value as "albama" now i want list of the values 'city' of all those five listings which has state as "albama" but i also want that reduntant city doesnt not showup. i.e. if 3 of cities are "florence" then they only showup once.
I have a custom post type named 'lawyer' and it contains various custom fields. two of the custom fields are 'state' and 'city'. i want to create a archive page or simple page(anyone will do) which can list all the values of city for specific state. for example i have 10 listings. 5 of which have custom field 'state' value as "albama" now i want list of the values 'city' of all those five listings which has state as "albama" but i also want that reduntant city doesnt not showup. i.e. if 3 of cities are "florence" then they only showup once.
Share Improve this question asked Aug 27, 2019 at 15:00 Chinmay RamraikaChinmay Ramraika 11 Answer
Reset to default 0The first thing I would do, if I were you, would be to change the data structure so that the location would a hierarchical custom taxonomy where states were parent terms and cities their child terms.
Then you could create a custom taxonomy template for your location taxonomy. In this template you'd display list of child terms when the queried object is a parent term.
EDIT 3.9.2019
Here is a very basic example how to get the child terms in archive-{post_type}.php
template
// WP_Term object
$current_term = get_queried_object();
// maybe array of WP_Term objects
$children = get_terms( array(
'taxonomy' => $current_term->taxonomy,
'parent' => $current_term->term_id
) );
// If any child terms exists
if ( $children && ! is_wp_error( $children ) ) : ?>
<ul>
<?php foreach ( $children as $child_term ) : ?>
<li><?php echo esc_html( $child_term->name ); ?></li>
<?php endforeach; ?>
</ul>
<?php endif;
A good place to learn more about custom post types is the Plugin Handbook, https://developer.wordpress/plugins/post-types/working-with-custom-post-types/
And if you're not very familiar with PHP yet, have a look at the PHP documentation Getting Started guide, https://www.php/manual/en/getting-started.php, or use your preferred search engine to find tutorials and beginner level courses.
本文标签:
版权声明:本文标题:Creating an archive page or simple template to list all values of a custom field of specific post type listing 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745190930a2646906.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论