0

I have a custom hierarchical taxonomy and I want to list in a dropdown menu all the top level "category" of the taxonomy that have post.

In my functions.php I've added this code:

function get_terms_dropdown_tm( $taxonomies, $args ) {
    $args       = array(
        'orderby'    => 'name',
        'show_count' => 1,
        'order'      => 'DESC',
        'hide_empty' => 1
    );
    $myterms    = get_terms( $taxonomies, $argo );
    $output     = "<select name='tassonomia_marca'>";
    $output    .= "<option value='#'>Seleziona la marca</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;
}

In the page, where I invoke this functions I've this code.

$taxonomies = array( 'tassonomia_marca' );
$select     = get_terms_dropdown_tm( $args, $taxonomies );
$select     = preg_replace( "#<select([^>]*)>#", "<select$1 onchange='return this.form.submit()'>", $select );
echo $select;

The problem is that WP doesn't take the $args and refill the dropdown menu with all the categories and without the post count

2
  • Could it be that when you're calling get_terms() in your function, it's passing $argo instead of $args as a typo?
    – Howdy_McGee
    Commented May 16, 2018 at 14:32
  • Hy! Thanks for your reply. I've corrected the typo, but the function still seems to work ignoring the $args
    – Fibonacci
    Commented May 16, 2018 at 14:38

1 Answer 1

0

There's a few things wrong with your code here.

  1. Your call to the function has the parameters mixed up. $args are passed first to the function but in your function definition they are expected to be passed second.
  2. The typo that you've fixed of $argo to $args
  3. In your functions foreach loop you're generating a lot of things that you just aren't using.

Here I've simplified your code to only take 1 taxonomy instead of multiple taxonomies. It calls wp_parse_args() so you can pass nothing to the $args parameter or anything you want to override the default array. The foreach loop you only need the slug and title as far as I can tell. Finally we wrap the loop with a conditional check just to make sure we have terms to loop through:

function get_terms_dropdown_tm( $taxonomy, $args = array() ) {

    $args = wp_parse_args( $args, array(
        'orderby'    => 'name',
        'show_count' => 1,
        'order'      => 'DESC',
        'hide_empty' => 1
    ) );
    $myterms    = get_terms( $taxonomy, $args );
    $output     = sprintf( '<select name="%1$s">', esc_attr( sanitize_text_field( $taxonomy ) ) );
    $output    .= "<option value='#'>Seleziona la marca</option>";

    if( ! is_wp_error( $myterms ) ) {

        foreach( $myterms as $term ) {
            $output .= sprintf( '<option value="%1$s">%2$s</option>', $term->slug, $term->name );
        }

    }

    $output .= "</select>";

    return $output;
}

The call:

$taxonomy   = 'tassonomia_marca';
$select     = get_terms_dropdown_tm( $taxonomy );
$select     = preg_replace( "#<select([^>]*)>#", "<select$1 onchange='return this.form.submit()'>", $select );
echo $select;
1
  • 1
    Thanks again for you reply. You've give me a big help, your code works. Thanks!
    – Fibonacci
    Commented May 16, 2018 at 15:34

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.