【问题标题】:Contact Form 7 and Custom post type联系表格 7 和自定义帖子类型
【发布时间】:2022-01-26 20:29:59
【问题描述】:

我想在 Wordpress 中使用联系表 7 来构建订单。我希望使用自定义帖子类型“贸易展览材料”的内容填充订单表格的内容 - 帖子类型包含字段“名称”“编号”“描述”“照片”。我们的想法是每件作品都可以从表格中选择。任何人都可以为此提供总体方向吗?我应该完全使用另一个插件吗?

【问题讨论】:

  • 如果您在自定义帖子类型中使用自定义字段,请使用重力形式。

标签: wordpress custom-post-type contact-form-7


【解决方案1】:

也许您可以为此使用wpcf7_form_tag 过滤器挂钩。

如果您想使用自定义帖子类型作为下拉菜单(选择)的选项,您可以在您的 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);

您可以在表单中添加字段:

[select* your-field-name include_blank]

在上面的示例中,post_title 用于下拉选项中。您可以在此处添加自己的字段(姓名、编号、描述、照片)。

【讨论】:

  • 只需确认这仍然适用于 WP 4.2 + CF7 。谢谢。
  • 有效,知道如何添加后期缩略图吗?
  • 知道为什么 _raw_{field_name} 信息对此不起作用吗?执行工作正常,但这些变量根本没有发挥作用。我什至添加了$pipes = new WPCF7_Pipes($tag['raw_values']); $tag['pipes'] = $pipes;,但并不高兴 - 电子邮件仅显示电子邮件地址部分。
  • 如何获取所选帖子的永久链接?
【解决方案2】:

我认为 wpcf7_form_tag 的工作方式与 vicente 在他之前的出色回答中所展示的方式不同。自 2015 年以来,情况可能发生了变化。

如果您阅读此处,它将解释您需要如何使用 wpcf7_form_tag:https://contactform7.com/2015/01/10/adding-a-custom-form-tag/

考虑到这一点以及联系表格 7 中的其他帖子:https://contactform7.com/2015/02/27/using-values-from-a-form-tag/#more-13351

我想出了这段代码来为我拥有的自定义帖子类型创建一个自定义下拉列表。

add_action('wpcf7_init', 'custom_add_form_tag_customlist');

function custom_add_form_tag_customlist() {
    wpcf7_add_form_tag( array( 'customlist', 'customlist*' ), 
'custom_customlist_form_tag_handler', true );
}

function custom_customlist_form_tag_handler( $tag ) {

    $tag = new WPCF7_FormTag( $tag );

    if ( empty( $tag->name ) ) {
        return '';
    }

    $customlist = '';

    $query = new WP_Query(array(
        'post_type' => 'CUSTOM POST TYPE HERE',
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'orderby'       => 'title',
        'order'         => 'ASC',
    ));

    while ($query->have_posts()) {
        $query->the_post();
        $post_title = get_the_title();
        $customlist .= sprintf( '<option value="%1$s">%2$s</option>', 
esc_html( $post_title ), esc_html( $post_title ) );
    }

    wp_reset_query();

    $customlist = sprintf(
        '<select name="%1$s" id="%2$s">%3$s</select>', $tag->name,
    $tag->name . '-options',
        $customlist );

    return $customlist;
}

然后你像这样在联系表单 7 中使用标签。

[customlist your-field-name]

希望这可以帮助像我一样正在寻找方法的其他人。

您可以更改它以从自定义帖子类型中获取您需要的任何信息。

但它没有任何验证。

【讨论】:

  • 这太棒了 - 这个解决方案对我来说就像一个魅力 - 复制 - 粘贴 - 包含 - 输入 CPT - 工作!非常感谢!
【解决方案3】:

Clyde Thomas 代码仍然运行良好,谢谢!

在我的情况下,我需要来自插件而不是帖子的数据,所以我修改了删除 WP_query 和 while 的代码

                    global $wpdb;
                    $result = $wpdb->get_results("SELECT title FROM wp_asl_stores ORDER BY title ASC ");
                    foreach($result as $row) {
                        $customlist .= sprintf( '<option value="%1$s">%2$s</option>',
                esc_html( $row->title ), esc_html( $row->title ) );
                    }

【讨论】:

  • 这最好留下作为评论克莱德托马斯所做的帖子与一个新的答案。
猜你喜欢
  • 2017-06-23
  • 2014-04-03
  • 1970-01-01
  • 2016-11-29
  • 2014-05-16
  • 2016-08-06
  • 1970-01-01
  • 1970-01-01
  • 2013-09-01
相关资源
最近更新 更多