dqf98772 2014-11-05 06:25
浏览 60
已采纳

在前端使用WPAlchemy重复字段

I'm using WPAlchemy to create meta data for my custom post types.

One of my custom post types, 'vendor', is declared as $vendor_mb with the prefix of _vendor in WPAlchemy and has below meta fields.

  • Address
  • Country
  • Zipcode
  • Website
  • Contacts <-- this is a repeating field created by have_fields_and_multi.

The 'Contacts' is a repeating field, created as below;

<!-- Repeating Fields for Contacts -->
<h4>Contacts</h4>

<?php while($mb->have_fields_and_multi('contacts')): ?>
<?php $mb->the_group_open(); ?>

    <label>Contact Information</label>

    <?php $mb->the_field('department'); ?>
    <span>Department</span>
    <p><input type="text" name="<?php $mb->the_name(); ?>" value="<?php $mb->the_value(); ?>"/></p>

    <?php $mb->the_field('firstname'); ?>
    <span>First name</span>
    <p><input type="text" name="<?php $mb->the_name(); ?>" value="<?php $mb->the_value(); ?>"/></p>

     <?php $mb->the_field('lastname'); ?>
    <span>Last name</span>
    <p><input type="text" name="<?php $mb->the_name(); ?>" value="<?php $mb->the_value(); ?>"/></p>

    <?php $mb->the_field('title'); ?>
    <span>Title</span>
    <p><input type="text" name="<?php $mb->the_name(); ?>" value="<?php $mb->the_value(); ?>"/></p>     

    <?php $mb->the_field('email'); ?>
    <span>Email</span>
    <p><input type="text" name="<?php $mb->the_name(); ?>" value="<?php $mb->the_value(); ?>"/></p>     

    <?php $mb->the_field('mobile'); ?>
    <span>Mobile</span>
    <p><input type="text" name="<?php $mb->the_name(); ?>" value="<?php $mb->the_value(); ?>"/></p>     

    <?php $mb->the_field('phone'); ?>
    <span>Phone</span>
    <p><input type="text" name="<?php $mb->the_name(); ?>" value="<?php $mb->the_value(); ?>"/></p>     

    <?php $mb->the_field('fax'); ?>
    <span>Fax</span>
    <p><input type="text" name="<?php $mb->the_name(); ?>" value="<?php $mb->the_value(); ?>"/></p>     

    <?php $mb->the_field('remark'); ?>
    <span>Remark</span>
    <p><input type="text" name="<?php $mb->the_name(); ?>" value="<?php $mb->the_value(); ?>"/></p>             
<?php $mb->the_group_close(); ?>
<?php endwhile; ?>

Using this code, I have no problem adding meta data to the meta fields, including repeating fields, in Wordpress Admin UI.

The problem is how to use the data in the front-end (I'm uploading a csv file containing multiple contacts information - John Doe 1, John Doe 2, and John Doe 3.)

I tried to use add_post_meta, and it creates three _vendor_contacts. I also tried to use update_post_meta, and the latter contact overwrites the former contact, leaving only the last one contact in the _vendor_contacts.

I further tried to get the existing value inside _vendor_contacts before doing add_post_meta or update_post_meta as below, but it doesn't work and doesn't echo anything.

global $vendor_mb;
$vendor_mb->the_meta();
while( $vendor_mb->have_fields_and_multi( 'contacts' ) ) :
$vendor_mb->the_group_open();
    $vendor_mb->the_value( 'firstname' );
    $vendor_mb->the_value( 'lastname' );
$vendor_mb->the_group_close();
endwhile;

So, any example I can refer to where WPAlchemy repeating fields are used in front-end?

Thank you.

  • 写回答

1条回答 默认 最新

  • dongshi1869 2015-01-22 01:28
    关注

    Because WP Alchemy stores data as a serialized array, you have to do some weird stuff to prepare to insert repeating fields.

    This article explains in detail what you need to do: http://www.2scopedesign.co.uk/wpalchemy-and-front-end-posts/

    The short answer is that you need to insert a serialized array, and then insert each meta value individually, like this:

     //add a serialised array for wpalchemy to work
    $data = array('reviews_what_we_did','reviews_star_rating','reviews_name','reviews_email','reviews_staff_member');
    $str = $data;
    update_post_meta( $pid, '_single_reviews_meta_fields', $str );
    
    //update the individual fields
    add_post_meta($pid, 'reviews_what_we_did', $service);
    add_post_meta($pid, 'reviews_star_rating', $rating);
    add_post_meta($pid, 'reviews_name', $name);
    add_post_meta($pid, 'reviews_email', $email);
    add_post_meta($pid, 'reviews_staff_member', $staff);
    

    However, this snippet only works for normal fields, not for repeating fields. Repeating fields add another layer of complexity.

    This article explains how to insert data into repeating fields: https://wpalchemists.com/2014/09/wpalchemy-update_post_meta/

    In your case, you would need to do something like this:

    $fields = array('address', 'country', 'zipcode', 'website', 'contacts');
    $str = $fields;
    update_post_meta( $postid, '_contact_meta_fields', $str );
    
    $contactfields = array(
    array(
    'department' => $departmentvalue,
    'firstname' => $firstnamevalue,
    'lastname'  => $lastnamevalue,
    'title' => $titlevalue,  // etc. - add all of your fields to the array
    )
    );
    
    add_post_meta($postid, 'address', $addressvalue); // do this for each of your non-repeating fields
    add_post_meta($postid, 'contacts', $contactfields); // this adds the repeating fields
    

    It's really helpful to print_r the post meta so that you can see how WPAlchemy creates a serialized array as well as each meta value.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 从Freecad中宏下载的DesignSPHysics,出现如下问题是什么原因导致的(语言-python)
  • ¥30 notepad++ 自定义代码补全提示
  • ¥15 MATLAB有限差分法解一维边值问题
  • ¥200 内网渗透测试 横向渗透 Windows漏洞 Windows权限维持
  • ¥15 数据结构图的相关代码实现
  • ¥15 python中aiohttp.client_exceptions.ContentTypeError
  • ¥30 DeepLung肺结节检测生成最大froc值对应的epoch报错
  • ¥15 信号发生器如何将频率调大,步尽值改成10
  • ¥15 keil 5 编程智能家具,风扇台灯开关,人体感应等
  • ¥100 找一名渗透方面的专家