I have set up a custom post type and taxonomy. The post type is called Local Services and the hierarchical taxonomy is called Regions. The Regions have child categories called Areas. I have set up a custom page to display my results. So far I have managed to display the latest 10 posts. But what I NEED to do is display one result on this page. To do this I need to have a drop down menu of regions and then according to which region is picked, this would then display a second drop down menu of the child areas. The user would then select one area.
I would like to display the result on my custom template page so when a visitor goes to the page initially, there would be a brief intro description with the drop down menu of Regions.
This sounds relatively simple and I looked at similar questions here but can't find a solution.
Can anyone help please?
This is what I have so far in my custom page template which displays a single result (the most recent). But I would like my page to display a drop down list of regions and another with their associated areas so a user will then get a single result displaying their chosen area. The page should not display any custom post information until a user has made these 2 choices.
$args = array(
'post_type' => 'local_services',
'posts_per_page' => 1
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
the_title();
echo '<div class="entry-content">';
the_content();
echo '</div>';
endwhile;
UPDATE: 12 DECEMBER
My custom post and taxonomy code:
<?php
function create_post_type() {
register_post_type( 'local_services',
array(
'labels' => array(
'name' => __( 'Local Services' ),
'singular_name' => __( 'Local Service' ),
'edit_item' => __( 'Edit Local Service' ),
'update_item' => __( 'Update Local Service' ),
),
'public' => true,
'menu_position' => 5,
'rewrite' => array('slug' => 'local-services')
)
);
}
add_action( 'init', 'create_post_type' );
function region_taxonomy() {
register_taxonomy(
'region',
'local_services',
array(
'hierarchical' => true,
'label' => 'Region',
'query_var' => true,
'rewrite' => array('slug' => 'region')
)
);
}
add_action( 'init', 'region_taxonomy' );
?>
UPDATE: 18 DECEMBER
I looked at the wp_dropdown_categories and found some code to customise it. I decided to go for a simpler option of just one dropdown instead of two. I created a new custom post type "services" and a new taxonomy "area".
I put the new code in my sidebar:
<?php
$categories = get_categories('taxonomy=area');
$select = "<select name='cat' id='cat' class='postform'>n";
$select.= "<option value='-1'>Select your local area</option>n";
foreach($categories as $category){
if($category->count > 0){
$select.= "<option value='".$category->slug."'>".$category->name."</option>";
}
}
$select.= "</select>";
echo $select;
?>
<script type="text/javascript"><!--
var dropdown = document.getElementById("cat");
function onCatChange() {
if ( dropdown.options[dropdown.selectedIndex].value != -1 ) {
location.href = "<?php echo home_url();?>/services/"+dropdown.options[dropdown.selectedIndex].value+"/";
}
}
dropdown.onchange = onCatChange;
--></script>
I also created a custom template "single-services.php" to display the result content. As I only want a single result per query this works great.