I have this code, it works fine.
But I need to "Show All" as an option.
<?php $term = get_queried_object();
$children = get_terms( $term->taxonomy, array(
'parent' => $term->term_id,
'hide_empty' => false
) ); ?>
<!-- If category is parent, list it -->
<?php if($children) { ?>
<?php
//first get the current term
$current_term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
//then set the args for wp_dropdown_categories
$orderby = 'name';
$args = array(
'child_of' => $current_term->term_id,
'taxonomy' => $current_term->taxonomy,
'hide_empty' => 1,
'hierarchical' => true,
'depth' => 2,
'title_li' => '',
'orderby'=> $orderby,
'show_option_all' => 'All',
);
?>
<form action="<?php bloginfo('url'); ?>" method="get">
<div>
<?php $cats = get_categories($args); ?>
<select id="categories">
<?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()
{
window.location = jQuery(this).val();
});
});
</script>
<?php } ?>
Luis Abarca answers:
Just add it as an option
<select id="categories">
<strong><option value="all">Show all</option></strong>
<?php foreach ($cats as $cat) : ?>
<option value="<?php echo get_term_link($cat, $cat->taxonomy) ?>"><?php echo $cat->name ?></option>
<?php endforeach; ?>
</select>
Luis Abarca comments:
You may also use the bult in function [[LINK href="http://codex.wordpress.org/Function_Reference/wp_dropdown_categories"]]wp_dropdown_categories[[/LINK]]
$args = array(
'child_of' => $current_term->term_id,
'taxonomy' => $current_term->taxonomy,
'hide_if_empty' => 1,
'hierarchical' => true,
'depth' => 2,
'orderby'=> $orderby,
'show_option_all' => 'Show All',
);
wp_dropdown_categories( $args );
vieradel comments:
Louis, Adding this line:
<option value="all">Show all</option>
can be seen "Show All".
But keeps URL erroneous when selected . Error 404
Remy answers:
The show all option should link to your default posts (or CPT) archive. I can't say what's yours without seeing your website, so I inputed the default website url. You can replace it
<?php $term = get_queried_object();
$children = get_terms( $term->taxonomy, array(
'parent' => $term->term_id,
'hide_empty' => false
) ); ?>
<!-- If category is parent, list it -->
<?php if($children) { ?>
<?php
//first get the current term
$current_term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
//then set the args for wp_dropdown_categories
$orderby = 'name';
$args = array(
'child_of' => $current_term->term_id,
'taxonomy' => $current_term->taxonomy,
'hide_empty' => 1,
'hierarchical' => true,
'depth' => 2,
'title_li' => '',
'orderby'=> $orderby,
'show_option_all' => 'All',
);
?>
<form action="<?php bloginfo('url'); ?>" method="get">
<div>
<?php $cats = get_categories($args); ?>
<select id="categories">
<?php foreach ($cats as $cat) : ?>
<option value="<?php bloginfo('url'); ?>">Show all</option>
<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()
{
window.location = jQuery(this).val();
});
});
</script>
<?php } ?>