Contact Form 7 and Custom post type

前端 未结 2 1401
粉色の甜心
粉色の甜心 2021-01-12 11:49

I want to use contact form 7 in Wordpress to build a order Form. I want the content of the order Form to be populated with content from a custom post type \"trade Show Mater

2条回答
  •  旧巷少年郎
    2021-01-12 12:04

    Maybe you can use the wpcf7_form_tag filter hook for this.

    If you want to use a custom post type as the options of a dropdown (select) you can add something like the example below in your functions.php:

    function dynamic_field_values ( $tag, $unused ) {
    
        if ( $tag['name'] != 'your-field-name' )
            return $tag;
    
        $args = array (
            'numberposts'   => -1,
            'post_type'     => 'your-custom-post-type',
            'orderby'       => 'title',
            'order'         => 'ASC',
        );
    
        $custom_posts = get_posts($args);
    
        if ( ! $custom_posts )
            return $tag;
    
        foreach ( $custom_posts as $custom_post ) {
    
            $tag['raw_values'][] = $custom_post->post_title;
            $tag['values'][] = $custom_post->post_title;
            $tag['labels'][] = $custom_post->post_title;
    
        }
    
        return $tag;
    
    }
    
    add_filter( 'wpcf7_form_tag', 'dynamic_field_values', 10, 2);
    

    In your form you can add the field:

    [select* your-field-name include_blank]
    

    In the example above the post_title is used in the options of the dropdown. You can add your own fields here (name, number, description, photo).

提交回复
热议问题