如何将自定义域值拉入并自动显示到联系人表单

时间:2018-08-08 作者:Swati

我正在使用contact for 7创建表单。目前,我正在从自定义帖子类型标题中自动填写文本名称字段下的值,请参见以下内容:

$post_ids = new WP_Query(array(
\'post_type\' => \'Buyers\', // replace with CPT name
\'fields\' => \'fname\' // replace with custom field name
));
 $name = array();
// go through each of the retrieved ids and get the title
if ($post_ids->have_posts()):
    foreach( $post_ids->posts as $id):
        // get the post title, and apply any filters which plugins may have added
        // (get_the_title returns unfiltered value)
        $name[] = apply_filters(\'the_title\', get_the_title($id));
    endforeach;
endif;



 <script>
$( "#autocomplete" ).autocomplete({
  source: <?php echo json_encode($name); ?>
});
</script>
现在,我想根据上面选择的帖子标题,用帖子中的自定义字段值自动填充其他表单字段(如姓氏、地址、联系人信息等)。我们如何才能做到这一点?

我想要以下内容:

<label> Your Name </label>
<input type="text" id="name" name="name" /> 
<input readonly="readonly" type="text" id="iprice" name="iprice" size="5">
    <input readonly="readonly" type="text" id="icode" name="icode" size="3">
[submit "Send"]


$(function() { 
      $(\'#iprice\').val("");
      $(\'#icode\').val("");
  $("#name").autocomplete({
    source: [{"label":"Air Soft Gun","price":"212","abbrev":"BMW"},
         {"label":"Pepsi Cola Hat","price":"24","abbrev":"CRY"},
         {"label":"Candle Lights Dinner","price":"780","abbrev":"NSS"},
         {"label":"Pork Meat Ball","price":"178","abbrev":"SZK"},
         {"label":"Granny Health Supplement","price":"24","abbrev":"TYT"}],
    minLength: 2,
    select: function(event, ui) { 
      $(\'#iprice\').val(ui.item.price);
      $(\'#icode\').val(ui.item.abbrev);
    }
  });

  $["ui"]["autocomplete"].prototype["_renderItem"] = function( ul, item) {
    return $( "<li></li>" ) 
      .data( "item.autocomplete", item )
      .append( $( "<a></a>" ).html( item.label ) )
      .appendTo( ul );
  };

});
标签将是我的文章标题,并基于所选的文章倾斜,在价格和icode下显示自定义字段值。

任何帮助都将不胜感激。提前谢谢你。

2 个回复
最合适的回答,由SO网友:Swati 整理而成

我想出来了。以下是解决方案:

表单代码:

<label> Your Name </label>
    <input type="text" id="name" name="name" />
        <input readonly="readonly" type="text" id="iprice" name="iprice" size="5">
PHP代码

  <?php
    $post_id = array(
        \'post_type\' => \'your_post_type_name\'
    );
    $post_data = array();
    $my_query = new WP_Query( $post_id );
    if ( $my_query->have_posts() ) {
        while ( $my_query->have_posts() ) {
            $my_query->the_post();
            $post_data[] = array(
                \'label\' => get_the_title($post_id->ID),
                \'price\' => get_post_meta( get_the_ID(), \'custom_field name\', true )
            );
        }
    }
    ?>
我的脚本代码:

<script type="text/javascript">
    $(function() { 
      $(\'#iprice\').val("");
      $("#name").autocomplete({
        source: <?php echo json_encode( $post_data); ?>,
        minLength: 2,
        select: function(event, ui) { 
          $(\'#iprice\').val(ui.item.price);
        }
      });

      $["ui"]["autocomplete"].prototype["_renderItem"] = function( ul, item) {
        return $( "<li></li>" ) 
          .data( "item.autocomplete", item )
          .append( $( "<a></a>" ).html( item.label ) )
          .appendTo( ul );
      };

    });
    </script>

SO网友:Balas

可以这样尝试:

foreach( $post_ids->posts as $id):
    // get the post title, and apply any filters which plugins may have added
    // (get_the_title returns unfiltered value)
    $name[] = apply_filters(\'the_title\', get_the_title($id));
    $lname[] = get_post_meta($id, \'last_name\', TRUE);
    $address[] = get_post_meta($id, \'address\', TRUE);
    $cinfo[] = get_post_meta($id, \'contact_info\', TRUE);
endforeach;
$data = [\'fname\'=>$name, \'lname\'=>$lname, \'address\'=>$address, \'cinfo\'=>$cinfo];
<script>
    $( "#name" ).autocomplete({
      source: <?php echo json_encode($data[\'fname\']]); ?>
    });
    $( "#lname" ).autocomplete({
      source: <?php echo json_encode($data[\'lname\']]); ?>
    });
    $( "#address" ).autocomplete({
      source: <?php echo json_encode($data[\'address\']]); ?>
    });
    $( "#cinfo" ).autocomplete({
      source: <?php echo json_encode($data[\'cinfo\']]); ?>
    });
</script>

结束

相关推荐

如何将自定义域值拉入并自动显示到联系人表单 - 小码农CODE - 行之有效找到问题解决它

如何将自定义域值拉入并自动显示到联系人表单

时间:2018-08-08 作者:Swati

我正在使用contact for 7创建表单。目前,我正在从自定义帖子类型标题中自动填写文本名称字段下的值,请参见以下内容:

$post_ids = new WP_Query(array(
\'post_type\' => \'Buyers\', // replace with CPT name
\'fields\' => \'fname\' // replace with custom field name
));
 $name = array();
// go through each of the retrieved ids and get the title
if ($post_ids->have_posts()):
    foreach( $post_ids->posts as $id):
        // get the post title, and apply any filters which plugins may have added
        // (get_the_title returns unfiltered value)
        $name[] = apply_filters(\'the_title\', get_the_title($id));
    endforeach;
endif;



 <script>
$( "#autocomplete" ).autocomplete({
  source: <?php echo json_encode($name); ?>
});
</script>
现在,我想根据上面选择的帖子标题,用帖子中的自定义字段值自动填充其他表单字段(如姓氏、地址、联系人信息等)。我们如何才能做到这一点?

我想要以下内容:

<label> Your Name </label>
<input type="text" id="name" name="name" /> 
<input readonly="readonly" type="text" id="iprice" name="iprice" size="5">
    <input readonly="readonly" type="text" id="icode" name="icode" size="3">
[submit "Send"]


$(function() { 
      $(\'#iprice\').val("");
      $(\'#icode\').val("");
  $("#name").autocomplete({
    source: [{"label":"Air Soft Gun","price":"212","abbrev":"BMW"},
         {"label":"Pepsi Cola Hat","price":"24","abbrev":"CRY"},
         {"label":"Candle Lights Dinner","price":"780","abbrev":"NSS"},
         {"label":"Pork Meat Ball","price":"178","abbrev":"SZK"},
         {"label":"Granny Health Supplement","price":"24","abbrev":"TYT"}],
    minLength: 2,
    select: function(event, ui) { 
      $(\'#iprice\').val(ui.item.price);
      $(\'#icode\').val(ui.item.abbrev);
    }
  });

  $["ui"]["autocomplete"].prototype["_renderItem"] = function( ul, item) {
    return $( "<li></li>" ) 
      .data( "item.autocomplete", item )
      .append( $( "<a></a>" ).html( item.label ) )
      .appendTo( ul );
  };

});
标签将是我的文章标题,并基于所选的文章倾斜,在价格和icode下显示自定义字段值。

任何帮助都将不胜感激。提前谢谢你。

2 个回复
最合适的回答,由SO网友:Swati 整理而成

我想出来了。以下是解决方案:

表单代码:

<label> Your Name </label>
    <input type="text" id="name" name="name" />
        <input readonly="readonly" type="text" id="iprice" name="iprice" size="5">
PHP代码

  <?php
    $post_id = array(
        \'post_type\' => \'your_post_type_name\'
    );
    $post_data = array();
    $my_query = new WP_Query( $post_id );
    if ( $my_query->have_posts() ) {
        while ( $my_query->have_posts() ) {
            $my_query->the_post();
            $post_data[] = array(
                \'label\' => get_the_title($post_id->ID),
                \'price\' => get_post_meta( get_the_ID(), \'custom_field name\', true )
            );
        }
    }
    ?>
我的脚本代码:

<script type="text/javascript">
    $(function() { 
      $(\'#iprice\').val("");
      $("#name").autocomplete({
        source: <?php echo json_encode( $post_data); ?>,
        minLength: 2,
        select: function(event, ui) { 
          $(\'#iprice\').val(ui.item.price);
        }
      });

      $["ui"]["autocomplete"].prototype["_renderItem"] = function( ul, item) {
        return $( "<li></li>" ) 
          .data( "item.autocomplete", item )
          .append( $( "<a></a>" ).html( item.label ) )
          .appendTo( ul );
      };

    });
    </script>

SO网友:Balas

可以这样尝试:

foreach( $post_ids->posts as $id):
    // get the post title, and apply any filters which plugins may have added
    // (get_the_title returns unfiltered value)
    $name[] = apply_filters(\'the_title\', get_the_title($id));
    $lname[] = get_post_meta($id, \'last_name\', TRUE);
    $address[] = get_post_meta($id, \'address\', TRUE);
    $cinfo[] = get_post_meta($id, \'contact_info\', TRUE);
endforeach;
$data = [\'fname\'=>$name, \'lname\'=>$lname, \'address\'=>$address, \'cinfo\'=>$cinfo];
<script>
    $( "#name" ).autocomplete({
      source: <?php echo json_encode($data[\'fname\']]); ?>
    });
    $( "#lname" ).autocomplete({
      source: <?php echo json_encode($data[\'lname\']]); ?>
    });
    $( "#address" ).autocomplete({
      source: <?php echo json_encode($data[\'address\']]); ?>
    });
    $( "#cinfo" ).autocomplete({
      source: <?php echo json_encode($data[\'cinfo\']]); ?>
    });
</script>

相关推荐