WordPress.org

Forums

Developer's Custom Fields
Creating a for loop to dynamically populate 'fields' (1 post)

  1. Sean
    Member
    Posted 2 years ago #

    Hello

    A while ago Steve mentioned (in the context of having repeatable fields) that it should be possible to dynamically generate the fields array in slt_cf_register_box() from a for loop - to create a given number of meta boxes without having to write them all out. I've been trying to do this but failing! Any chance you could point me in the right direction?

    So, for example, if I wanted to generate 15 sets of title/text in a box:

    $fields = array();
    for ($i=0; $i <= 15; $i++) {
    $fields[] =
          array(
                  'name'          => 'title_'.$i,
                  'label'         => 'Title '.$i,
                  'type'          => 'text',
                  'scope'         => array( 'page', 'project', 'except_posts' => array( 4, 45 ) ),
                  'capabilities'  => array( 'edit_posts' )
              ),
          array(
                  'name'          => 'text_'.$i,
                  'label'         => 'Text '.$i,
                  'type'          => 'textarea',
                  'autop'         => 'true',
                  'scope'         => array( 'page', 'project', 'except_posts' => array( 4, 45 ) ),
                  'capabilities'  => array( 'edit_posts' )
              )
    };
    
    slt_cf_register_box( array(
        'type'      => 'post',
        'title'     => 'Process Map',
        'id'        => 'process_map',
        'context'   => 'normal',
        'priority'  => 'high',
        'fields'    => $fields
    ));

    Thanks!

    http://wordpress.org/extend/plugins/developers-custom-fields/

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • Developer's Custom Fields
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic