I wanna display posts of my custom taxonomy in a dropdown list, I mean I create a filter for taxonomies in my custom post type I have 2 different types which contains some sub-category & in that category I added some posts, the first taxonomy is "Types" and the second one is "Categories", so when I click the taxonomy dropdown and select the sub-category for displaying the posts, nothing displays...
function diwp_arts_custom_post_type(){
$labels = array(
'name' => 'Arts',
'singular_name' => 'Art',
'add_new' => 'Add New Post',
'add_new_item' => 'Enter Art Details',
'all_items' => 'All Arts',
'featured_image' => 'Add Feature Image',
'set_featured_image' => 'Set Feature Image',
'remove_featured_image' => 'Remove Feature Image'
);
$args = array(
'public' => true,
'label' => 'Arts',
'labels' => $labels,
'description' => 'Arts is a collection of all Art and their info',
'menu_icon' => 'dashicons-art',
'supports' => array( 'title', 'editor', 'thumbnail'),
'capability_type' => 'page',
);
register_post_type('arts', $args);
}
add_action( 'init', 'diwp_arts_custom_post_type' );
function wpdocs_create_book_tax() { register_taxonomy( 'Types', 'arts', array( 'label' => __( 'Types', 'textdomain' ), 'rewrite' => array( 'slug' => 'arts' ), 'hierarchical' => true, ) );
register_taxonomy( 'Categories', 'arts', array(
'label' => __( 'Categories', 'textdomain' ),
'rewrite' => array( 'slug' => 'arts' ),
'hierarchical' => true,
) );
}
add_action( 'init', 'wpdocs_create_book_tax', 0 );
function get_terms_dropdown_grade_level($taxonomies, $args){ $myterms = get_terms($taxonomies, $args); $output =""; //I can change this name (for my remembreance)! $output .="Select Types"; 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 .="".$term_name.""; } $output .=""; return $output; }
function get_terms_dropdown_type($taxonomies, $args){
$myterms = get_terms($taxonomies, $args);
$output ="<select name='Categories'>";
$output .="<option value=''>Select Categories</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;
}
I write this code into the sidebar.php file because I wanna show dropdown on all pages:
" method="get"> 'name','hide_empty'=>false); $select = get_terms_dropdown_grade_level($taxonomies, $args); $select = preg_replace("#]*)>#", "", $select); echo $select; ?> '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="Go" />
</div>
I wanna display posts of my custom taxonomy in a dropdown list, I mean I create a filter for taxonomies in my custom post type I have 2 different types which contains some sub-category & in that category I added some posts, the first taxonomy is "Types" and the second one is "Categories", so when I click the taxonomy dropdown and select the sub-category for displaying the posts, nothing displays...
function diwp_arts_custom_post_type(){
$labels = array(
'name' => 'Arts',
'singular_name' => 'Art',
'add_new' => 'Add New Post',
'add_new_item' => 'Enter Art Details',
'all_items' => 'All Arts',
'featured_image' => 'Add Feature Image',
'set_featured_image' => 'Set Feature Image',
'remove_featured_image' => 'Remove Feature Image'
);
$args = array(
'public' => true,
'label' => 'Arts',
'labels' => $labels,
'description' => 'Arts is a collection of all Art and their info',
'menu_icon' => 'dashicons-art',
'supports' => array( 'title', 'editor', 'thumbnail'),
'capability_type' => 'page',
);
register_post_type('arts', $args);
}
add_action( 'init', 'diwp_arts_custom_post_type' );
function wpdocs_create_book_tax() { register_taxonomy( 'Types', 'arts', array( 'label' => __( 'Types', 'textdomain' ), 'rewrite' => array( 'slug' => 'arts' ), 'hierarchical' => true, ) );
register_taxonomy( 'Categories', 'arts', array(
'label' => __( 'Categories', 'textdomain' ),
'rewrite' => array( 'slug' => 'arts' ),
'hierarchical' => true,
) );
}
add_action( 'init', 'wpdocs_create_book_tax', 0 );
function get_terms_dropdown_grade_level($taxonomies, $args){ $myterms = get_terms($taxonomies, $args); $output =""; //I can change this name (for my remembreance)! $output .="Select Types"; 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 .="".$term_name.""; } $output .=""; return $output; }
function get_terms_dropdown_type($taxonomies, $args){
$myterms = get_terms($taxonomies, $args);
$output ="<select name='Categories'>";
$output .="<option value=''>Select Categories</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;
}
I write this code into the sidebar.php file because I wanna show dropdown on all pages:
" method="get"> 'name','hide_empty'=>false); $select = get_terms_dropdown_grade_level($taxonomies, $args); $select = preg_replace("#]*)>#", "", $select); echo $select; ?> '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="Go" />
</div>
Share
Improve this question
edited Jan 22, 2021 at 6:10
Moiz Aslam
asked Jan 18, 2021 at 7:30
Moiz AslamMoiz Aslam
34 bronze badges
1 Answer
Reset to default 0I cant see where in the code you want to have a taxonomy drop-down, and also what you want to do with this drop down. That also depends what values you want to use as option values.
But generally you can use wp_dropdown_categories
to create a custom taxonomy drop-down.
wp_dropdown_categories( array(
'show_option_all' => 'Show all arts',
'taxonomy' => 'arts', // Name of the taxonomy or taxonomies to retrieve
'name' => 'arts', // Value for the 'name' attribute of the select element
'orderby' => 'name', // Order drop-down options
'value_field' => 'slug', // Term field that should be used to populate the 'value' attribute of the option elements
// Accepts: 'term_id', 'name', 'slug', 'term_group', 'term_taxonomy_id', 'taxonomy', 'description', 'parent', 'count'
'selected' => $selected, // Value of the option that should be selected. You need to add your own custom $selected
'hierarchical' => true // Whether to traverse the taxonomy hierarchy
) );