Answer the question
In order to leave comments, you need to log in
How to get the term_id of the parent from get_term() and pass this value to child_of to another form?
Good afternoon everyone, please help me with the script.
There is a small form
AND a custom taxonomy "city"
It's the only one.
<select id="region">
<?php
$city=get_terms( 'city', 'orderby=name&hide_empty=0&parent=25');
foreach ($city as $term){
echo "<option value='{$term->term_id}'>{$term->name}</option>";} ?>
</select>
<select id="city">
<?php
$city=get_terms( 'city', 'orderby=name&hide_empty=0&child_of=');
foreach ($city as $term){
echo "<option value='{$term->term_id}'>{$term->name}</option>";} ?>
</select>
Answer the question
In order to leave comments, you need to log in
The first select is loaded in the usual way, PHP generates a list of terms of one parent ("All cities"). Next, there should be ajax - on the 'change' event of the select of these terms, you send a request to admin-ajax.php, create a handler for the wp_ajax_{your_action} hook, to which you pass the ID of the selected term, and return a list of child terms.
PS: If this is on the frontend, then you need to hang up on 2 hooks - wp_ajax_{your_action} and wp_ajax_nopriv_{your_action}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question