Let me explain the problem I am dealing with:
I have 1 custom taxonomy named "province" and 1 custom-post-type named "pizzerie".
I need the taxonomy items displayed in a drop-down.
When I select a taxonomy item the second dropdown must display and links all the titles of the custom-post-type items based on what taxonomy item is selected. And when I select a title It will open the relative custom-post. It is similar to State and Cities. When I select the State in a first dropodown the relative Cities will display in a second dropdown.
Best regards, Antonio
Daniel Yoen answers:
try this :
<?php wp_dropdown_categories('taxonomy=custom_taxonomy_name'); ?>
Daniel Yoen comments:
Depends on your previous Post, you need this :
<form role="search" method="get" id="searchform" action="<?php bloginfo('home'); ?>">
<div>
<input type="text" value="" name="s" id="s" />
<?php
function get_terms_dropdown($taxonomies, $args){
$myterms = get_terms($taxonomies, $args);
$optionname = "optionname";
$emptyvalue = "";
$output ="<select name='".$optionname."'><option selected='".$selected."' value='".$emptyvalue."'>Select a Category</option>'";
foreach($myterms as $term){
$term_taxonomy=$term->YOURTAXONOMY; //CHANGE ME
$term_slug=$term->slug;
$term_name =$term->name;
$link = $term_slug;
$output .="<option name='".$link."' value='".$link."'>".$term_name."</option>";
}
$output .="</select>";
return $output;
}
$taxonomies = array('YOURTAXONOMY');
$args = array('order'=>'ASC','hide_empty'=>true);
echo get_terms_dropdown($taxonomies, $args);
?>
<input type="submit" id="searchsubmit" value="Search" />
</div>
</form>
natspace comments:
Well I insert your:
<script type="text/javascript">
$(document).ready(function(){
$("#json_options").change(function(){
var json_option = $("#json_options").val();
$("#json_post").empty().append("<option>loading...</option>"); //show loading...
var json_url = "<?php echo get_template_directory_uri(); ?>/index-json.php?json_option=" + encodeURIComponent(json_option);
$.getJSON(json_url,function(data){
$("#json_post").empty(); //clear states selections
if(data==""){
$("#json_post").append("<option value=\"0\">No states found</option>");
}
else{
for(i=0; i<data.id.length; i++){
$("#json_post").append("<option value=\"" + data.id[i] + "\">" + data.name[i] + "</option>");
}
}
});
return false;
});});
</script>
<?php
function get_terms_dropdown($taxonomies, $args)
{
$myterms = get_terms($taxonomies, $args);
foreach ($myterms as $term)
{
$term_taxonomy = $term->taxonomy;
$term_slug = $term->slug;
$term_name = $term->name;
$link = $term_slug;
$output .= "<option value=" . $term_slug . ">" . $term_name . "</option>";
}
return $output;
}
$taxonomies = array('province'); // REPLACE WITH YOUR OWN TAXONOMY
$args = array('orderby' => 'count','hide_empty' => true);
?>
<select name="json_options" id="json_options">
<option value="0">Select here</option>
<?php echo get_terms_dropdown($taxonomies, $args); ?>
</select>
<select name="json_post" id="json_post"></select>
In my custom template and add your index-json.php in my theme directory.
The first dropdown works fine, infact It displays all my taxonomy values. But when I selec a taxonomy value the second dropdown doesn't display the relative custom-post titles.
Kailey Lampert answers:
I just tested this and it worked
function get_terms_dropdown() {
echo '<form class="searchcat" role="search" method="get" id="searchform" action="' . home_url() .'">';
echo'<div class="rowElem">';
echo'<input type="text" value="" name="s" id="s" placeholder="search here..." tabindex="1" />';
echo'<input type="submit" id="searchsubmit" value="Search" tabindex="2" />';
echo'</div></form>'; //form tag closes before <select>?
$taxonomies = array('pizzerie');//change Your_Texonomy to yours
$args = array('orderby'=>'count', 'hide_empty'=>true );
$myterms = get_terms( $taxonomies, $args );
$output = "<select>";
foreach($myterms as $term){
$term_taxonomy=$term->taxonomy;
$term_name =$term->name;
$link = get_term_link( $term, $term_taxonomy);
$output .= "<option value='{$link}'>{$term_name}</option>";
}
$output .= "</select>";
echo $output;
}
And could you clarify "nothing happens"? Does that really mean that <strong>nothing</strong> happens, or just that what you're expecting doesn't happen?
Arnav Joy answers:
change here
$taxonomies = array('pizzerie');//change Your_Texonomy to yours
to
$taxonomies = array('category');//change Your_Texonomy to yours
check if it is called now
Arnav Joy comments:
one more thing
change hide_empty to false as follows:-
$args = array('orderby'=>'count','hide_empty'=>false)
so new function will look like..
function get_terms_dropdown() {
echo '<form class="searchcat" role="search" method="get" id="searchform" action="' . get_bloginfo('url') .'">';
echo'<div class="rowElem">';
echo'<input type="text" value="" name="s" id="s" placeholder="search here..." tabindex="1" />';
$taxonomies = array('pizzerie');//change Your_Texonomy to yours
$args = array('orderby'=>'count','hide_empty'=>false);
echo'<input type="submit" id="searchsubmit" value="Search" tabindex="2" />';
echo'</div></form>';
$myterms = get_terms($taxonomies, $args);
$output ="<select>";
foreach($myterms as $term){
$root_url = get_bloginfo('url');
$term_taxonomy=$term->taxonomy;
$term_slug=$term->slug;
$term_name =$term->name;
$link = $root_url.'/'.$term_taxonomy.'/'.$term_slug;
$output .="<option value='".$link."'>".$term_name."</option>";
}
$output .="</select>";
echo ($output);
}
add_action('init', 'get_terms_dropdown');