function list_term_locations_shortcode() {
$terms = get_terms([
'taxonomy' => 'regions_loca',
'hide_empty' => false,
'parent' => 0,
]);
if (empty($terms) || is_wp_error($terms)) {
return '<p>no data</p>';
}
ob_start();
?>
<div class="dth-list-term">
<ul>
<?php foreach ($terms as $term) : ?>
<li>
<a href="<?php echo get_term_link($term); ?>"><?php echo esc_html($term->name); ?></a>
<?php
$child_terms = get_terms([
'taxonomy' => 'regions_loca',
'hide_empty' => false,
'parent' => $term->term_id,
]);
if (!empty($child_terms) && !is_wp_error($child_terms)) :
?>
<ul>
<?php foreach ($child_terms as $child) : ?>
<li>
<a href="<?php echo get_term_link($child); ?>"><?php echo esc_html($child->name); ?></a>
<?php
$sub_child_terms = get_terms([
'taxonomy' => 'regions_loca',
'hide_empty' => false,
'parent' => $child->term_id,
]);
if (!empty($sub_child_terms) && !is_wp_error($sub_child_terms)) :
?>
<ul>
<?php foreach ($sub_child_terms as $sub_child) : ?>
<li><a href="<?php echo get_term_link($sub_child); ?>"><?php echo esc_html($sub_child->name); ?></a></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
</div>
<?php
return ob_get_clean();
}
add_shortcode('list_term_locations', 'list_term_locations_shortcode');
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter