WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Posts category as selected in wp_dropdown_categories (4 posts)

  1. Pete
    Member
    Posted 1 year ago #

    I'm displaying a drop down list of categories on a front end form using this code. I want the category which is set in the post to be selected when the form loads but this isn't working. What wrong with the code?

    $category = get_the_category();
    $selected = 0;
    if ( $category ) {
    $selected = $category[0]->cat_name;
    }
    wp_dropdown_categories( 'hierarchical=1&hide_empty=0&orderby=name&id=cat&show_count=0&title_li=&use_desc_for_title=1&class=cat requiredField&selected=' . $selected );
  2. andrewmills
    Member
    Posted 1 year ago #

    The selected argument of the wp_dropdown_categories needs to be an integer (i.e. the id of the category).

    Try changing this: $selected = $category[0]->cat_name;

    to this: $selected = $category[0]->cat_ID;

  3. andrewmills
    Member
    Posted 1 year ago #

    Just following up. Did that resolve your issue?

  4. Pete
    Member
    Posted 1 year ago #

    Yes thank you

Topic Closed

This topic has been closed to new replies.

About this Topic