WordPress.org

Ready to get started?Download WordPress

Forums

Multi-directional array in custom post type? (4 posts)

  1. SpankMarvin
    Member
    Posted 2 years ago #

    Hi all

    I am trying to incorporate some custom attributes to a dish post type I am making. There are some standard custom post data elements that are no problem. However, I'd like to offer, through jQuery, an expandable list of inputs so that admin can add custom callouts - as many as they want.

    My form code is looking like this:

    <label>Abbreviation (e.g. gf for Gluten Free): </label><input type="text" name="custom_callouts[][abbrev]" size="20" value="<?php esc_attr_e($abbr); ?>" /><br />
    				<label>Description: </label><input type="text" name="custom_callouts[][desc]" size="100" value="<?php esc_attr_e($desc); ?>" />

    with the two variables declared within a foreach loop that grabs the custom_callouts data from the database.

    When I print the callouts data, I'm getting expected results for 'desc' only. Each iteration of 'custom_callouts' ends up being an array with 'desc' = 'whatever'. The values are correct, but 'abbrev' appears not to be written at all.

    Any suggestions much appreciated!

    John

  2. alchymyth
    Forum Moderator
    Posted 2 years ago #

    can you paste the full code, including all of the foreach loop?

  3. SpankMarvin
    Member
    Posted 2 years ago #

    Yes, I'm sorry. The code below is the pertinent code from a function which is called by add_meta_box:

    global $post;
    $custom = get_post_custom($post->ID);
    $add_callouts = unserialize($custom['custom_callouts'][0]);
    if( ( is_array($add_callouts) ) && (count($add_callouts) > 0) ):
      foreach($add_callouts as $key){
        $abbr = $key['abbrev'];
        $desc = $key['desc']; ?>
    <div>
      <label>Abbreviation (e.g. gf for Gluten Free): </label><input type="text" name="custom_callouts[][abbrev]" size="20" value="<?php esc_attr_e($abbr); ?>" /><br />
      <label>Description: </label><input type="text" name="custom_callouts[][desc]" size="100" value="<?php esc_attr_e($desc); ?>" />
    </div>
    <?php
      unset($abbr, $desc);
     } // end foreach
    
    endif; ?>
  4. SpankMarvin
    Member
    Posted 2 years ago #

    My arrays in the forms were stupid. Makes far more sense to name them custom_callouts_abbrev[] and custom_callouts_desc[] and deal with them as 2 separate arrays. This will surely work. Thank you for your time anyway!

    J

Topic Closed

This topic has been closed to new replies.

About this Topic