最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

custom taxonomy - Contact Form 7 - Populating dropdown list with terms relative to the post

programmeradmin1浏览0评论

I found another post that had the code for dynamically populating a CF7 dropdown with the terms of a taxonomy, and I have it working, but I wanted the terms to not just be all within the taxonomy, but I wanted them to only be relative to the post that it's on.

To give it some more context, this is an application form on a job posting. The custom post type are job listings. I have a taxonomy of locations. Those terms are assigned to a post. I want only the selected terms to appear in this dropdown.

This is the code I have:

function dynamic_select_list( $tag ) {

    // Only run on select lists
    if( 'select' !== $tag['type'] && ('select*' !== $tag['type']) ) {
        return $tag;
    } else if ( empty( $tag['options'] ) ) {
        return $tag;
    }

    $term_args = array();

    // Loop thorugh options to look for our custom options
    foreach( $tag['options'] as $option ) {

        $matches = explode( ':', $option );

        if( ! empty( $matches ) ) {

            switch( $matches[0] ) {

                case 'taxonomy':
                    $term_args['taxonomy'] = $matches[1];
                    break;

                case 'parent':
                    $term_args['parent'] = intval( $matches[1] );
                    break;

            }
        }

    }

    // Ensure we have a term arguments to work with
    if( empty( $term_args ) ) {
        return $tag;
    }

    // Merge dynamic arguments with static arguments
    $term_args = array_merge( $term_args, array(
        'hide_empty' => false,
    ) );

    $terms = get_terms( $term_args );

    // Add terms to values
    if( ! empty( $terms ) && ! is_wp_error( $term_args ) ) {

        foreach( $terms as $term ) {

            $tag['values'][] = $term->name;

        }

    }

    return $tag;

}
add_filter( 'wpcf7_form_tag', 'dynamic_select_list', 10 );

I found another post that had the code for dynamically populating a CF7 dropdown with the terms of a taxonomy, and I have it working, but I wanted the terms to not just be all within the taxonomy, but I wanted them to only be relative to the post that it's on.

To give it some more context, this is an application form on a job posting. The custom post type are job listings. I have a taxonomy of locations. Those terms are assigned to a post. I want only the selected terms to appear in this dropdown.

This is the code I have:

function dynamic_select_list( $tag ) {

    // Only run on select lists
    if( 'select' !== $tag['type'] && ('select*' !== $tag['type']) ) {
        return $tag;
    } else if ( empty( $tag['options'] ) ) {
        return $tag;
    }

    $term_args = array();

    // Loop thorugh options to look for our custom options
    foreach( $tag['options'] as $option ) {

        $matches = explode( ':', $option );

        if( ! empty( $matches ) ) {

            switch( $matches[0] ) {

                case 'taxonomy':
                    $term_args['taxonomy'] = $matches[1];
                    break;

                case 'parent':
                    $term_args['parent'] = intval( $matches[1] );
                    break;

            }
        }

    }

    // Ensure we have a term arguments to work with
    if( empty( $term_args ) ) {
        return $tag;
    }

    // Merge dynamic arguments with static arguments
    $term_args = array_merge( $term_args, array(
        'hide_empty' => false,
    ) );

    $terms = get_terms( $term_args );

    // Add terms to values
    if( ! empty( $terms ) && ! is_wp_error( $term_args ) ) {

        foreach( $terms as $term ) {

            $tag['values'][] = $term->name;

        }

    }

    return $tag;

}
add_filter( 'wpcf7_form_tag', 'dynamic_select_list', 10 );
Share Improve this question asked Feb 15, 2020 at 23:33 Darren BachanDarren Bachan 3752 gold badges7 silver badges17 bronze badges 4
  • have you tried to use developer.wordpress/reference/functions/get_the_terms ? – Michael Commented Feb 16, 2020 at 2:07
  • @Michael I am unsure of how to tie that into cf7 – Darren Bachan Commented Feb 16, 2020 at 2:59
  • I am referring to this one line in your posted code: ` $terms = get_terms( $term_args );` - PS: have you asked in the plugin's forum? – Michael Commented Feb 16, 2020 at 3:15
  • Replaced $terms = get_terms( $term_args ); with $terms = get_the_terms( $post->ID, 'location_categories' ); and it works. – Darren Bachan Commented Feb 16, 2020 at 18:20
Add a comment  | 

1 Answer 1

Reset to default 1

Use the dynamic_dropdown tag functionality offered by the Smart Grid layout extension. The dynamic tag can take 3 sources of data (a given taxonomy, or branch within that taxonomy, a list of post titles, or the results of a filtered hook function). Use the filter hook to populated your dropdown as,

add_filter('cf7sg_dynamic_dropdown_custom_options', 'filter_options',10,3);
function filter_options($options, $field_name, $form_key){
  if($form_key != 'my-form') return $options; //check this is the correct form.
  if($field_name != 'custom-dropdown') return $options; //check this is the correct field.
  $options = array(); 
  //get your terms
  $terms = get_terms( $term_args );
  // Add terms to values
  if( ! empty( $terms ) && ! is_wp_error( $term_args ) ) {

    foreach( $terms as $term ) {
      //this is the <option value="">label</opton> value->label pairs.
      $options[$term->id] = $term->name;
    }
  }
  return $options;
}
发布评论

评论列表(0)

  1. 暂无评论