• Resolved Reload Web

    (@ahmed17)


    Hi

    Below is code i am using in widget but i can’t get option of select field. I see document guide select field type require associative array so get_search_course_cat function also return associative array term id as keys, term names as values but i am fail to get select field with taxonomy option. Please guide me


    array(
    ‘title’ => array(
    ‘type’ => ‘text’,
    ‘label’ => __(‘Title’, ‘edupress’),
    ‘default’ => __(‘Search By Category’, ‘text-domain’),
    ),
    ‘course_cat’ => array(
    ‘type’ => ‘select’,
    ‘label’ => __( ‘Choose a thing from a long list of things’, ‘edupress’ ),
    ‘multiple’ => true,
    ‘options’ => $this->get_search_course_cat(),
    ),

    ),
    function get_search_course_cat()
    {
    return get_terms( ‘course_category’, ‘orderby=name&fields=id=>name&hide_empty=0’ );

    }
    `

    https://www.ads-software.com/plugins/siteorigin-panels/

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Contributor alexgso

    (@alexgso)

    Hi ahmed17,

    I don’t think you’ll be able to use get_terms() directly as it returns invalid data. As per the docs, you’ll want your array to be structured like so:

    array(
    ‘field_key’ => __( ‘Option name’, ‘widget-form-fields-text-domain’ ),
    ‘field_key_2’ => __( ‘Other Option’, ‘widget-form-fields-text-domain’ ),
    )

    Ignoring that issue, get_terms() by default will return an object. Please refer to the codex for more information on how to return an associative array

    Plugin Contributor alexgso

    (@alexgso)

    Hi ahmed17,

    Sorry! I forgot to include proper code formatting.

    array(
      'field_key' => __( 'Option name', 'widget-form-fields-text-domain' ),
      'field_key_2' => __( 'Other Option', 'widget-form-fields-text-domain' ),
    )
    Thread Starter Reload Web

    (@ahmed17)

    I am using get_terms function not get_term it’s return single term object by term id.

    https://developer.www.ads-software.com/reference/functions/get_terms/ .

    You can see $args argument list in above codex url there is fields argument ‘id=>name’ (returns an associative array with ids as keys, term names as values), i double check it’s return array like

    array(
    ‘1’ => ‘Development’,
    ‘2’ => ‘Mobile Apps’,
    )

    `

    Thanks
    Ahmed

    Plugin Contributor alexgso

    (@alexgso)

    This thread is being handled at our official support forums.

    Thread Starter Reload Web

    (@ahmed17)

    Thanks Alex

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How can i get custom taxonomy select dropdown as field in widget’ is closed to new replies.