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>
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 ?
Hi i have this dropdown select option when selecting change the other value of the select dropdown. Im using onchange in jquery. My problem is when i select apple dropdown the lists of apple products will appear which is okay. Now when i select samsung it wont change the value for samsung products. Here is my code below
<script type="text/javascript">
jQuery(document).ready(function($) {
/*$("#phoneBrand").change(function () {
var end = this.value;
var firstDropVal = $('#data-original-index').val();
alert(firstDropVal);
});*/
$("#phoneBrand").on('change', function(){
var phoneBrand = $("#phoneBrand").val();
if(phoneBrand == "apple"){
alert(phoneBrand);
<?php
// All Option
//echo '<option value="">'.$houzez_local['all_model'].'</option>';
$prop_type = get_terms (
array(
"property_feature"
),
array(
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => true,
'parent' => 0
)
);
//houzez_hirarchical_options('property_feature', $prop_type, $type );
?>
//var pModel = "<?php echo $pModel;?>";
//alert(pModel);
$("div").each(function(i) {
var dropdown = $("<select>");
<?php foreach($prop_type as $model): ?>
<?php
$pName = $model->name;
$pSlug = $model->slug;
$pModel = $model->description;
?>
<?php if($pModel == "apple"): ?>
dropdown.append($('<option>', {
value:'<?php echo $pSlug; ?>',
text:'<?php echo $pName;?>'
}));
<?php endif; ?>
<?php endforeach; ?>
$("#phoneModel").replaceWith(dropdown);
});
}else if(phoneBrand == "blackberry"){
//$("#age_boys").show();
alert('test');
}else if(phoneBrand == "htc"){
}else if(phoneBrand == "huawai"){
}else if(phoneBrand == "lg"){
}else if(phoneBrand == "microsoft"){
}else if(phoneBrand == "nokia"){
}else if(phoneBrand == "samsung"){
alert(phoneBrand);
<?php
// All Option
//echo '<option value="">'.$houzez_local['all_model'].'</option>';
$prop_type = get_terms (
array(
"property_feature"
),
array(
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => true,
'parent' => 0
)
);
//houzez_hirarchical_options('property_feature', $prop_type, $type );
?>
//var pModel = "<?php echo $pModel;?>";
//alert(pModel);
$( "div" ).each(function(i) {
var dropdown = $("<select id='samsung'>");
<?php foreach($prop_type as $model): ?>
<?php
$pName = $model->name;
$pSlug = $model->slug;
$pModel = $model->description;
?>
<?php if($pModel == "samsung"): ?>
dropdown.append($('<option>', {
value:'<?php echo $pSlug; ?>',
text:'<?php echo $pName;?>'
}));
<?php endif; ?>
<?php endforeach; ?>
$("#phoneModel").replaceWith(dropdown);
});
}
});
});
</script>
Now my html select dropdown which is built from theme houzez in wordpress
<?php if( $adv_show_hide['type'] != 1 ) { ?>
<div class="col-sm-3 col-xs-12">
<div class="form-group" >
<select id="phoneModel" name="feature" class="selectpicker " data-live-search="false" data-live-search-style="begins">
<?php
// All Option
echo '<option value="">'.$houzez_local['all_model'].'</option>';
$prop_type = get_terms (
array(
"property_feature"
),
array(
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => true,
'parent' => 0
)
);
houzez_hirarchical_options('property_feature', $prop_type, $type);
?>
</select>
</div>
</div>
<?php } ?>
Can someone help me figured this thing out? that it will only change the value once the dropdown is only selected.
Provided below is a screenshot image
Any help is muchly appreciated. TIA. Im using wordpress
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'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>
I have this code from the interwebs about how to duplicate a field in a form.
Now the problem is that this form is a text field, but I would like to have a drop down list with all pages from my website.
This would be easy if it only was in PHP since I don't know how to write jQuery
So is there anybody out there who is kind enough and willing to combine these two codes for me:
JQuery:
<script type="text/javascript">
var fieldname = <?php echo json_encode( $this->get_field_name('stream_sources') ) ?>;
var fieldnum = <?php echo json_encode( $stream_counter-1 ) ?>;
jQuery(function($) {
var count = fieldnum;
$('.<?php echo $this->get_field_id( 'add_field' );?>').click(function() {
$("#<?php echo $this->get_field_id( 'field_clone' );?>").append("<p><input type='text' name='"+fieldname+"["+(count+1)+"][title] value='' class='widefat sourc"+(count+1)+"'><span class='remove-field button button-primary button-large'>Verwijderen</span></p>");
count++;
});
$(".remove-field").live('click', function() {
$(this).parent().remove();
});
});
</script>
With this PHP:
<select name="meta-url-1" id="meta-url-1"><?php
global $post;
$args = array( 'numberposts' => -1);
$posts = get_pages($args);
echo '<option value="#"></option>';
foreach( $posts as $post ) : setup_postdata($post); ?>
<option value="<?php echo $post->post_name; ?>"><?php the_title(); ?></option>
<?php endforeach; ?>
</select>
Notice that the PHP is how I would write it. But I need to have the drop down in a repeatable field.
Hope this is possible && anyone would help.
M.
Your code is
<option value="<?php echo $post->post_name; ?>"><?php the_title(); ?></option>
In this case the value you are assigning in option will not repeat. You will need to assign a unique value to each option.
I suggest you to use a number variable to assign value.
Try below code:
<select name="meta-url-1" id="meta-url-1"><?php
global $post;
$args = array( 'numberposts' => -1);
$posts = get_pages($args);
$i = 0;
echo '<option value="#"></option>';
foreach( $posts as $post ) : setup_postdata($post); ?>
<option value="<?php echo $i++; ?>"><?php the_title(); ?></option>
<?php endforeach; ?>
</select>