I'm trying to create two dropdown boxes on a custom post type page called Activities. One taxonomy category is for Activities Grade Level and the other for Activities Type. With what I have now, if you leave one of the drop boxes empty the search will go to a 404 page. I used the code from this wordpress forum and so this is what I have in my functions folder:
function get_terms_dropdown_grade_level($taxonomies, $args){
$myterms = get_terms($taxonomies, $args);
$output ="<select name='activities_grade_level'>";
$output .="<option value='#'>Select grade level</option>";
foreach($myterms as $term){
$root_url = get_bloginfo('url');
$term_taxonomy=$term->taxonomy;
$term_slug=$term->slug;
$term_name =$term->name;
$link = $term_slug;
$output .="<option value='".$link."'>".$term_name."</option>";
}
$output .="</select>";
return $output;
}
function get_terms_dropdown_type($taxonomies, $args){
$myterms = get_terms($taxonomies, $args);
$output ="<select name='activities_type'>";
$output .="<option value='#'>Select activity type</option>";
foreach($myterms as $term){
$root_url = get_bloginfo('url');
$term_taxonomy=$term->taxonomy;
$term_slug=$term->slug;
$term_name =$term->name;
$link = $term_slug;
$output .="<option value='".$link."'>".$term_name."</option>";
}
$output .="</select>";
return $output;
}
And this is what I have on the post type archive page:
<h2>Filter by</h2>
<form action="<?php bloginfo('url'); ?>" method="get">
<div>
<?php
$taxonomies = array('activities_grade_level');
$args = array('orderby'=>'name','hide_empty'=>false);
$select = get_terms_dropdown_grade_level($taxonomies, $args);
$select = preg_replace("#<select([^>]*)>#", "<select$1 onchange='return this.form.submit()'>", $select);
echo $select;
?>
<?php
$taxonomies = array('activities_type');
$args = array('orderby'=>'name','hide_empty'=>false);
$select = get_terms_dropdown_type($taxonomies, $args);
$select = preg_replace("#<select([^>]*)>#", "<select$1 onchange='return this.form.submit()'>", $select);
echo $select;
?>
<input type="submit" name="submit" value="filter" />
</div>
</form>
Incase it's good information, I'll include the urls too. A regular url that leads to the correct search is
[...]/?activities_grade_level=elementary-school&activities_type=engineering&submit=filter
A url where one of the drop boxes is empty is
[...]/?activities_grade_level=%23&activities_type=engineering&submit=filter where %23 replaced something that could have been elementary-school or middle-school
A url that leads to the correct category would be
[...]/activities_type/engineering/
or
[...]/activities_grade_level/middle-school
Any suggestions for this php and wordpress beginner?
I'm trying to create two dropdown boxes on a custom post type page called Activities. One taxonomy category is for Activities Grade Level and the other for Activities Type. With what I have now, if you leave one of the drop boxes empty the search will go to a 404 page. I used the code from this wordpress forum and so this is what I have in my functions folder:
function get_terms_dropdown_grade_level($taxonomies, $args){
$myterms = get_terms($taxonomies, $args);
$output ="<select name='activities_grade_level'>";
$output .="<option value='#'>Select grade level</option>";
foreach($myterms as $term){
$root_url = get_bloginfo('url');
$term_taxonomy=$term->taxonomy;
$term_slug=$term->slug;
$term_name =$term->name;
$link = $term_slug;
$output .="<option value='".$link."'>".$term_name."</option>";
}
$output .="</select>";
return $output;
}
function get_terms_dropdown_type($taxonomies, $args){
$myterms = get_terms($taxonomies, $args);
$output ="<select name='activities_type'>";
$output .="<option value='#'>Select activity type</option>";
foreach($myterms as $term){
$root_url = get_bloginfo('url');
$term_taxonomy=$term->taxonomy;
$term_slug=$term->slug;
$term_name =$term->name;
$link = $term_slug;
$output .="<option value='".$link."'>".$term_name."</option>";
}
$output .="</select>";
return $output;
}
And this is what I have on the post type archive page:
<h2>Filter by</h2>
<form action="<?php bloginfo('url'); ?>" method="get">
<div>
<?php
$taxonomies = array('activities_grade_level');
$args = array('orderby'=>'name','hide_empty'=>false);
$select = get_terms_dropdown_grade_level($taxonomies, $args);
$select = preg_replace("#<select([^>]*)>#", "<select$1 onchange='return this.form.submit()'>", $select);
echo $select;
?>
<?php
$taxonomies = array('activities_type');
$args = array('orderby'=>'name','hide_empty'=>false);
$select = get_terms_dropdown_type($taxonomies, $args);
$select = preg_replace("#<select([^>]*)>#", "<select$1 onchange='return this.form.submit()'>", $select);
echo $select;
?>
<input type="submit" name="submit" value="filter" />
</div>
</form>
Incase it's good information, I'll include the urls too. A regular url that leads to the correct search is
[...]/?activities_grade_level=elementary-school&activities_type=engineering&submit=filter
A url where one of the drop boxes is empty is
[...]/?activities_grade_level=%23&activities_type=engineering&submit=filter where %23 replaced something that could have been elementary-school or middle-school
A url that leads to the correct category would be
[...]/activities_type/engineering/
or
[...]/activities_grade_level/middle-school
Any suggestions for this php and wordpress beginner?
Share Improve this question edited Jun 15, 2020 at 8:21 CommunityBot 1 asked Jul 2, 2013 at 23:23 anitaanita 1171 gold badge1 silver badge7 bronze badges 1- Take a look at the WCM filterama plugin (available for free on GitHub). It does exactly that, but only on the admin side. – kaiser Commented Mar 2, 2014 at 11:47
1 Answer
Reset to default 1Right, seems a little empty here but I figured out the problem myself, again. The trick was to have empty values in the $output variables.
So
$output .="<option value=''>Select taxonomy #1</option>";
NOT
$output .="<option value='#'>Select taxonomy #1</option>";
This is how you can create two dropdown menus and filter out your posts using your custom taxonomies.
Paste this into your functions.php file
function get_terms_dropdown_grade_level($taxonomies, $args){
$myterms = get_terms($taxonomies, $args);
$output ="<select name='MYTAXONOMY#1'>"; //CHANGE ME!
$output .="<option value=''>Select taxonomy #1</option>"; //CHANGE ME TO YOUR LIKING!
foreach($myterms as $term){
$root_url = get_bloginfo('url');
$term_taxonomy=$term->taxonomy;
$term_slug=$term->slug;
$term_name =$term->name;
$link = $term_slug;
$output .="<option value='".$link."'>".$term_name."</option>";
}
$output .="</select>";
return $output;
}
function get_terms_dropdown_type($taxonomies, $args){
$myterms = get_terms($taxonomies, $args);
$output ="<select name='MYTAXONOMY#2'>"; //CHANGE ME!
$output .="<option value=''>Select taxonomy #2</option>"; //CHANGE ME TO YOUR LIKING! foreach($myterms as $term){
$root_url = get_bloginfo('url');
$term_taxonomy=$term->taxonomy;
$term_slug=$term->slug;
$term_name =$term->name;
$link = $term_slug;
$output .="<option value='".$link."'>".$term_name."</option>";
}
$output .="</select>";
return $output;
}
Paste this onto the page you want the dropdown menus to appear. (I put mine on a special archives page, like archive-activities.php.)
<h3>Filter by:</h3>
<form action="<?php bloginfo('url'); ?>" method="get">
<div>
<?php
$taxonomies = array('MYTAXONOMY#1'); //CHANGE ME!
$args = array('orderby'=>'name','hide_empty'=>false);
$select = get_terms_dropdown_grade_level($taxonomies, $args);
$select = preg_replace("#<select([^>]*)>#", "<select$1 onchange='return this.form.submit()'>", $select);
echo $select;
?>
<?php
$taxonomies = array('MYTAXONOMY#2'); //CHANGE ME!
$args = array('orderby'=>'name','hide_empty'=>false);
$select = get_terms_dropdown_type($taxonomies, $args);
$select = preg_replace("#<select([^>]*)>#", "<select$1 onchange='return this.form.submit()'>", $select);
echo $select;
?>
<input type="submit" name="submit" value="filter" /> <!--CHANGE VALUE TO YOUR LIKING!-->
</div>
</form>
Cheers! :)
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745481321a4629568.html
评论列表(0条)