Reputation: 873
I have the following code that displays a dropdown menu in my Wordpress site with the categories for posts and then displays a post. What I want to do is add an option for 'All Categories'. I don't want to use 'show_option_all' => 'All Categories',
as this puts the option at the top and I want it at the bottom, and this also gets auto selected when on some page but I won't go into that. Suffice to say I want to manually insert an option at the end of my list.
here is my code
<form id="category-select" class="category-select" action="<?php echo esc_url( home_url( '/' ) ); ?>" method="get">
<?php
$args = array(
'show_option_none' => __( 'Select Category' ),
'show_option_all' => 'All Categorys',
'show_count' => 1,
'orderby' => 'name',
'echo' => 0,
);
$select = wp_dropdown_categories( $args );
$replace = "<select$1 onchange='return this.form.submit()'>";
$select = preg_replace( '#<select([^>]*)>#', $replace, $select );
echo $select;
?>
<noscript>
<input type="submit" value="View" />
</noscript>
</form>
Thanks.
Any help appreciated.
Ian
Upvotes: 2
Views: 2477
Reputation: 427
Best option is to use get_terms() function and create category dropdown as per your requirement.
$terms = get_terms([
'taxonomy' => $taxonomy,
'hide_empty' => false,
]);
<select>
foreach($terms as $cat)
{
echo '<option value="'.$cat->term_id.'">$cat->name</option>';
}
<option value="" selected="selected">All Categories</option>
</select>
Upvotes: 1