0

I have a custom Post Type "Portfolio" with Custom Taxonomy "Portfolio Categories" and Terms within that Taxonomy are "Branding", "Print", "Web" and "Multimedia". Each of these have Child Terms.

I would like to display a separate drop down for each Term (Branding, Print, Web and Multimedia) that displays its Child Terms in a drop down. Once a Child Term is selected, it will immediately bring you to an archive of the Child Term without having to click a submit button.

The following code works for Categories but I can't figure out how to modify for the Taxonomy Terms.

<li id="categories">
    <h2><?php _e('Posts by Category'); ?></h2>
    <form action="<?php bloginfo('url'); ?>/" method="get">
    <div>
<?php
$select = wp_dropdown_categories('show_option_none=Select category&show_count=1&orderby=name&echo=0');
$select = preg_replace("#<select([^>]*)>#", "<select$1 onchange='return this.form.submit()'>", $select);
echo $select;
?>
    <noscript><div><input type="submit" value="View" /></div></noscript>
    </div></form>
</li>

Any help would be greatly appreciated.

OK, I have the following code that displays a list of child term links but it needs to be modified into a drop down.

<?php
$term_id = TERM_ID_HERE;
$taxonomy_name = 'TAXONOMY_NAME_HERE';
$termchildren = get_term_children( $term_id, $taxonomy_name );

echo '<ul>';
foreach ( $termchildren as $child ) {
    $term = get_term_by( 'id', $child, $taxonomy_name );
    echo '<li><a href="' . get_term_link( $child, $taxonomy_name ) . '">' .     $term->name . '</a></li>';
}
echo '</ul>';
?>
0

1 Answer 1

1

This actually quite an interesting question. I did this answer last night that gets a list of all taxonomies with a list of the terms belonging to that specific taxonomy. The code in there can very easily be adapted to what to want to do.

I also went and though of other methods and got googling for ideas and I came up with this idea from frankiejarrett.com. Why not create your own dropdown function like wp_dropdown_categories, which makes sense to me

Here is what Frankie explained

Method #2

That’s where Method #2 comes in. We’ll have to write our own custom function that will generate the dropdown so we can output each option value as a slug:

Here is the function for the custom dropdown function to list your terms

function fjarrett_custom_taxonomy_dropdown( $taxonomy ) {
    $terms = get_terms( $taxonomy );
    if ( $terms ) {
        printf( '<select name="%s" class="postform">', esc_attr( $taxonomy ) );
        foreach ( $terms as $term ) {
            printf( '<option value="%s">%s</option>', esc_attr( $term->slug ), esc_html( $term->name ) );
        }
        print( '</select>' );
    }
}

This can now be called in your template like

<?php fjarrett_custom_taxonomy_dropdown( 'my_custom_taxonomy' ); ?>

He further went and did a more comprehensive function

Expansions on Method #2

If you’re a coding rockstar you can take Method #2 even further by making room for more parameters. This will give you even more control and make it function more like wp_dropdown_categories does:

function fjarrett_custom_taxonomy_dropdown( $taxonomy, $orderby = 'date', $order = 'DESC', $limit = '-1', $name, $show_option_all = null, $show_option_none = null ) {
    $args = array(
        'orderby' => $orderby,
        'order' => $order,
        'number' => $limit,
    );
    $terms = get_terms( $taxonomy, $args );
    $name = ( $name ) ? $name : $taxonomy;
    if ( $terms ) {
        printf( '<select name="%s" class="postform">', esc_attr( $name ) );
        if ( $show_option_all ) {
            printf( '<option value="0">%s</option>', esc_html( $show_option_all ) );
        }
        if ( $show_option_none ) {
            printf( '<option value="-1">%s</option>', esc_html( $show_option_none ) );
        }
        foreach ( $terms as $term ) {
            printf( '<option value="%s">%s</option>', esc_attr( $term->slug ), esc_html( $term->name ) );
        }
        print( '</select>' );
    }
}

which you can call in your template as

<?php fjarrett_custom_taxonomy_dropdown( 'my_custom_taxonomy', 'date', 'DESC', '5', 'my_custom_taxonomy', 'Select All', 'Select None' ); ?>

I hope all of this helps to reach your goal

EDIT

To get the children of a term, you should use get_term_children. I've modified the code in the example to make it work in a dropdown

<form action="<?php bloginfo('url'); ?>/" method="get">

<?php
$term_id = 118;
$taxonomy_name = 'event_cat';
$termchildren = get_term_children( $term_id, $taxonomy_name );

echo '<select name="' . $taxonomy_name . '" onchange="this.form.submit()">';
echo '<option selected>All terms in ' . $taxonomy_name . '</option>';
foreach ( $termchildren as $child ) {
    $term = get_term_by( 'id', $child, $taxonomy_name );
    $link = get_term_link( $term, $taxonomy_name );
    echo '<option value="'.$term->slug.'"><a href="' .esc_url( $link ) . '">' . $term->name . '</a></option>';
}
echo '</select>';
?> 
<noscript><div><input type="submit" value="View" /></div></noscript>

</form> 
10
  • Hi Pieter, Thanks for your answer but unfortunately neither solution works for sub terms like I'm looking for. Commented Jun 20, 2014 at 15:04
  • Have a look at get_term_children. That should do the trick Commented Jun 20, 2014 at 15:30
  • Also check this post Commented Jun 20, 2014 at 18:44
  • Please see my edit Commented Jun 22, 2014 at 17:24
  • That did it Peter! I just needed to add the form tag at the beginning, which I just added to your answer. Thanks for your help on this :) Commented Jun 22, 2014 at 18:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.