I've updated my php code. I've managed to get all the parent taxonomies and their children in different select boxes. I'd like some help with the following: When I change the parent i'd like the second select to show his children only.
function categories_header_form()
{
?>
<div id="header-form">
<h3 class="form-title">
<?php echo 'Αναζήτηση προϊόντων ανά περιοχή' ?>
</h3>
<form id="search-form" action="#" method="post" >
<div class="form-container">
<?php nomoi(); ?>
<?php towns(); ?>
<?php products_selection(); ?>
<button type="submit" class="button" id="search-form-button">Εύρεση</button>
</div>
</form>
</div>
<?php
}
function products_selection()
{
$args = array(
'post_type' => 'seller',
'taxonomy' => 'category',
'hide_empty' => 0,
'exclude' => 1,1078,1079
);
$products = get_categories( $args );
if ( $products ) {
echo '<select id="products-select">';
echo '<option selected="" disabled="" value="0"><span>Προϊόντα</span></option>';
foreach ($products as $product) {
echo '<option class="product-name" id="'. $product->term_id .'">'. $product->name .'</option>';
}
echo '</select>';
}
}
function nomoi()
{
$args = array(
'post_type' => 'seller',
'taxonomy' => 'nomos',
'hide_empty'=> 0,
'parent' => 0
);
$categories = get_categories( $args );
if ( $categories ) {
// print_r($categories);
echo '<select id="nomoi-select">';
echo '<option selected="" disabled="" value="0"><span>Νομοί</span></option>';
foreach ($categories as $category) {
$id = $category->term_id;
$name = $category->name;
$taxonomy = $category->taxonomy;
echo '<option class="nomos" id="'. $id .'">'. $name .'</option>';
}
echo '</select>';
}
}
function towns()
{
$args = array(
'taxonomy' => 'nomos',
'hide_empty' => 0,
'hierarchical' => true,
'depth' => 1,
);
$cats = get_categories( $args );
echo '<select id="town-select">';
echo '<option selected="" disabled="" value="0"><span>Πόλεις</span></option>';
foreach ($cats as $cat) {
$cat_name = $cat->name;
$id = $cat->cat_ID;
echo '<option class="town" id="'. $id .'">'. $cat_name .'</option>';
}
echo '</select>';
}
Here is the most basic example i can give you..
You can improve this...
$("#nothingToSee").change(function() {
$("#sub1").css('display', 'none');
$("#sub2").css('display', 'none');
$("#sub3").css('display', 'none');
y = $(this).val();
$("#" + y).css('display', 'block');
});
#sub1 {
display: block;
}
#sub2,
#sub3 {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="nothingToSee" id="nothingToSee">
<option value="sub1">1</option>
<option value="sub2">2</option>
<option value="sub3">3</option>
</select>
<select name="nothingToSee" id="sub1">
<option value="1">1-1</option>
<option value="2">1-2</option>
<option value="3">1-3</option>
</select>
<select name="nothingToSee" id="sub2">
<option value="1">2-1</option>
<option value="2">2-2</option>
<option value="3">2-3</option>
</select>
<select name="nothingToSee" id="sub3">
<option value="1">3-1</option>
<option value="2">3-2</option>
<option value="3">3-3</option>
</select>
Related
i want to changes query_posts by clicking a link witch ajax and load new result in to my div
i use this code in single.php
By using the $_GET function at the top
if( !empty($_GET['key']) )
$meta_key=$_GET['key'];
else $meta_key= ''; // default
dropdown list
<form action="" method="get">
<select name="key" id="fromchange" class="style-select"onchange="if(this.value != 0) { this.form.submit(); }">
<option value="">""</option>
<option value="artist_artist" <?php if ($key == "artist_artist") { echo 'style="color:gray" selected'; } ?>> ACTOR</option>
<option value="artist_director" <?php if ($key == "artist_director") { echo 'style="color:gray" selected'; } ?>> DIRECTOR</option>
</select>
</form>
phpcode and div for new result is :
<div id="result">
<?php // ACTORS
$title1='>'. get_the_title() .'</a> ('.get_field('date').')';
$title2='>'. get_the_title() . ' ('.get_field('date').')';
$args_actor=array(
'post_type'=> 'post',
'cat' => '5',
'posts_per_page'=> -1,
'orderby' => 'title',
'order' => 'ASC',
);
$crew_query = null;
$crew_query = new WP_Query(array($args_actor,
'meta_query'=> array('relation' => 'OR',
array('key'=>$meta_key,'value'=> $title1,'compare'=>'LIKE'),
array('key'=>$meta_key,'value'=> $title2,'compare' => 'LIKE'))
));
if( $crew_query->have_posts() ) {
echo'<ul class="mpanel">';
while ($crew_query->have_posts()) : $crew_query->the_post();
include (TEMPLATEPATH . '/single-movie/content-list-movie-crew.php');
endwhile;echo '</ul>';}
else{
echo'<ul class="mpanel">';
include (TEMPLATEPATH . '/single-movie/movie-crew-else.php');
echo '</ul>';
}
wp_reset_query(); // Restore global post data stomped by the_post().
?>
</div>
how to run this code and change query by click with ajax in div ?
I have a custom taxonomy and I've added a category dropdown to the archive page to allow users to quickly switch between categories. I have the following code, which works in redirecting users to the new category that they choose. However, the dropdown always defaults to the first option in the <select>. How can I pass the selected <option> through and set it as the default value when a user switches categories?
<?php
$args = array(
'taxonomy' => 'custom_taxonomy',
'orderby' => 'name',
'show_count' => 1,
'hierarchical' => 1,
'title_li' => 'title'
);
<form id="categoriesform" action="<?php bloginfo('url'); ?>" method="post">
<div>
<?php $cats = get_categories($args); ?>
<select id="categories" name="custom_taxonomy">
<option value="allCategories">All Categories</option>
<?php foreach ($cats as $cat) : ?>
<option value="<?php echo get_term_link($cat, $cat->taxonomy) ?>">
<?php echo $cat->name ?></option>
<?php endforeach; ?>
</select>
</div>
</form>
<script>
jQuery(document).ready(function() {
jQuery('#categories').change(function() {
if (jQuery(this).val() == 'allCategories') {
window.location = 'http://example.com/all-categories';
} else {
window.location = jQuery(this).val();
}
});
});
</script>
First you need to get current category id via get_queried_object()->term_id; then you check and add selected in option.
<?php
$current=get_queried_object()->term_id;
$args = array(
'taxonomy' => 'custom_taxonomy',
'orderby' => 'name',
'show_count' => 1,
'hierarchical' => 1,
'title_li' => 'title'
);
<form id="categoriesform" action="<?php bloginfo('url'); ?>" method="post">
<div>
<?php $cats = get_categories($args); ?>
<select id="categories" name="custom_taxonomy">
<option value="allCategories">All Categories</option>
<?php foreach ($cats as $cat) : ?>
<option <?php echo ($current == $cat->term_id ? 'selected' : ''); ?> value="<?php echo get_term_link($cat, $cat->taxonomy) ?>">
<?php echo $cat->name ?></option>
<?php endforeach; ?>
</select>
</div>
</form>
<script>
jQuery(document).ready(function() {
jQuery('#categories').change(function() {
if (jQuery(this).val() == 'allCategories') {
window.location = 'http://example.com/all-categories';
} else {
window.location = jQuery(this).val();
}
});
});
</script>
i want to make dropdown for child categories, i have this code but it's not working ...
<select name="event-dropdown" onchange='document.location.href=this.options[this.selectedIndex].value;'>
<option value=""><?php echo attribute_escape(__('Select Event')); ?></option>
<?php
$categories= get_categories('child_of=3');
foreach ($categories as $cat) {
$option = '<option value="/category/archive/'.$cat->category_nicename.'">';
$option .= $cat->cat_name;
$option .= ' ('.$cat->category_count.')';
$option .= '</option>';
echo $option;
}
?>
</select>
Argument have to array use this
$categories = get_categories( array( 'child_of' => 10 );
instead of
$categories= get_categories('child_of=3');
thanks but it's not working, i found another solution
<li id="categories"><h2><?php _e( 'category-name' ); ?></h2>
<?php wp_dropdown_categories('include=3,10' ); ?>
<script type="text/javascript">
<!--
var dropdown = document.getElementById("cat");
function onCatChange() {
if ( dropdown.options[dropdown.selectedIndex].value > 0 ) {
location.href = "<?php echo esc_url( home_url( '/' ) ); ?>?cat="+dropdown.options[dropdown.selectedIndex].value;
}
}
dropdown.onchange = onCatChange;
-->
</script>
</li>
I faced very weird error, there is a post type "author", and I show the author list in post type "articles", the list shows all authors successfully and also save successfully, but when I want to show them in front end the author does not shown in loop but when I print_r("author") the array show me selected authors but in loop there is no author. Here is my code:
I also used foreach loop.
Functions.php
<?php
function ct_downlaod_meta($post){
$opt_meta_author = get_post_meta($post->ID, 'opt_meta_author', true);
echo '<select name="opt_meta_author[]" id="opt_meta_author" multiple="multiple">';
$val = get_post_meta($post->ID, 'opt_meta_author', true);
$q = get_posts('post_type=author&post_parent=0&numberposts=-1&orderby=menu_order&order=ASC');
$val_array = explode('<br />', $val); echo $val_array;
foreach ($q as $obj)
{
echo '<option value="'.$obj->post_title.'"'.selected($obj->post_title, $val);
if(in_array($obj->post_title, $val_array)) { echo ' selected="selected"'; };
echo '>'.$obj->post_title.'</option>';
}
echo '</select>';
}
add_action('save_post','save_download_meta_data');
function save_download_meta_data(){
global $post;
$opt_meta_author = implode('<br />',$_POST['opt_meta_author']);
update_post_meta( $post->ID, 'opt_meta_author', $opt_meta_author);
}
?>
author-bio.php
<?php
$opt_meta_author_array = explode('<br />', get_post_meta($post->ID, 'opt_meta_author', true));
$args = array(
'post__in' => $opt_meta_author_array,
'post_type' => 'author',
'orderby' => 'title',
'order' => 'ASC',
);
print_r($args);
$author = new WP_Query($args);
if($author->have_posts()) :
?>
<h3 id="entry-author-title"><?php _e('About The Author(s)', 'framework') ?></h3>
<?php
while($author->have_posts()) :
$author->the_post();
?>
<section id="entry-author" class="clearfix">
<div style=" margin-bottom:12px; float:left; width:100%; padding-bottom:5px;">
<div class="gravatar">
<?php
if(has_post_thumbnail()){
the_post_thumbnail( array( 70, 70 ) );
}
else{
echo '<img src="http://0.gravatar.com/avatar/6179f2642031d79f16bf2f03f0f66df9?s=70&d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D70&r=G" />';
}
?>
</div>
<?php /*?> <h4><?php echo the_title() ?></h4><?php */?>
<div style="width:100%; margin:0 0px;">
<?php //echo get_post_meta($post->ID, 'txt_meta_country', true); ?>
</div>
<div class="entry-author-desc">
<?php echo the_content() ?>
</div>
</div></section>
<?php
endwhile;
endif;
?>
When i print_r($args); the array give me true result
Array ( [post__in] => Array ( [0] => Andrew Turnell [1] => Adrian Gimpel ) [post_type] => author [orderby] => title [order] => ASC )
but in loop is empty result.
So one day delay i found a solution on my own in functions.php
<?php
function ct_downlaod_meta($post){
$opt_meta_author = get_post_meta($post->ID, 'opt_meta_author', true);
echo '<select name="opt_meta_author[]" id="opt_meta_author" multiple="multiple">';
$val = get_post_meta($post->ID, 'opt_meta_author', true);
$q = get_posts('post_type=author&post_parent=0&numberposts=-1&orderby=menu_order&order=ASC');
$val_array = explode('<br />', $val); echo $val_array;
foreach ($q as $obj)
{
echo '<option value="'.$obj->ID.'"'.selected($obj->ID, $val);
if(in_array($obj->ID, $val_array)) { echo ' selected="selected"'; };
echo '>'.$obj->post_title.'</option>';
}
echo '</select>';
}
add_action('save_post','save_download_meta_data');
function save_download_meta_data(){
global $post;
$opt_meta_author = implode('<br />',$_POST['opt_meta_author']);
update_post_meta( $post->ID, 'opt_meta_author', $opt_meta_author);
}
?>
in author-bio.php i not change any thing and my code is running well
I have two html selects with data from WordPress:
a) select list with cities
<?php
$args = array( 'child_of' => 50, 'meta_key' => 'E-mail' );
$pages = get_pages( $args );
echo '<select id="towns" name="towns">';
echo '<option value="" selected="selected">Please choose a town...</option>';
foreach($pages as $post) {
$page_id = get_the_ID();
$page_meta = get_post_meta(get_the_ID(),'E-mail',true);
$page_title = get_the_title($ID);
echo '<option value="' . $page_meta .'">'. $page_title . '</option>';
}
echo '</select>';
?>
b) select list with street names
<?php
//$page_id = 62;
echo '<select id="streets" streets="kierunki">';
echo '<option value="" selected="selected">Please choose a street</option>';
$tags = wp_get_post_tags($page_id);
foreach ($tags as $tag) {
echo '<option value="' . $tag->name . '">'. $tag->name . '</option>';
}
echo '</select>';
}
?>
Is it possible to pass value of $page_id to second select so when I choose for example first option in first select dropdown, second select list will appear with corresponding streets?