自定义POST类型Metabox数组

时间:2011-08-09 作者:Kaspars

我正在尝试使用自定义metabox数组创建自定义帖子类型。如何创建这种自动填充帖子类型?我想我必须使用二维数组。但如何自动填充数据数组?如何保存这种数组。

有人能给我指出正确的方向吗?

Please take a look at the images.

Img1

第二次imgenter image description here

迄今为止我的代码:

<?php
//add custom field - price
add_action("admin_init", "price");

function object_init(){
  add_meta_box("price_meta", "Price fields :", "object", "price_meta", "normal", "low");

}


function price_meta(){
 global $post;
  $custom = get_post_custom($post->ID);
  $price = $custom["price"][0];
  ?>

  <p style="float:left;"><label>Nr :</label><br />
    <input type="text" name="priceNr" size="10" value="<?php echo $price; ?>"/>
</p>
  <p style="float:left;"><label>Description :</label><br />
    <input type="text" name="priceD" size="50" value="<?php echo $price; ?>"/>
</p>
  <p style="float:left;"><label>Price :</label><br />
    <input type="text" name="price" size="20" value="<?php echo $price; ?>"/>
</p>

  <?php
}
//Save product price
add_action(\'save_post\', \'save_detailss\');

function save_detailss(){ 
global $post; 

$post_id = $post->ID; 

// to prevent metadata or custom fields from disappearing... 
if ( defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE ) 
return $post_id; 

update_post_meta($post_id, "price", $_POST["price"]); 

}   

?>

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

你的代码毫无意义,无论如何,这里有一个很好的方法,你可以得到:enter image description here

function Print_price_fileds($cnt, $p = null) {
if ($p === null){
    $a = $b = $c = \'\';
}else{
    $a = $p[\'n\'];
    $b = $p[\'d\'];
    $c = $p[\'p\'];
}
return  <<<HTML
<li>
    <label>Nr :</label>
    <input type="text" name="price_data[$cnt][n]" size="10" value="$a"/>

    <label>Description :</label>
    <input type="text" name="price_data[$cnt][d]" size="50" value="$b"/>

    <label>Price :</label>
    <input type="text" name="price_data[$cnt][p]" size="20" value="$c"/>
    <span class="remove">Remove</span>
</li>
HTML
;
}


//add custom field - price
add_action("add_meta_boxes", "object_init");

function object_init(){
  add_meta_box("price_meta_id", "Price fields :","price_meta", "post", "normal", "low");

}

function price_meta(){
 global $post;

  $data = get_post_meta($post->ID,"price_data",true);
  echo \'<div>\';
  echo \'<ul id="price_items">\';
  $c = 0;
    if (count($data) > 0){
        foreach((array)$data as $p ){
            if (isset($p[\'p\']) || isset($p[\'d\'])|| isset($p[\'n\'])){
                echo Print_price_fileds($c,$p);
                $c = $c +1;
            }
        }

    }
    echo \'</ul>\';

    ?>
        <span id="here"></span>
        <span class="add"><?php echo __(\'Add Price Data\'); ?></span>
        <script>
            var $ =jQuery.noConflict();
                $(document).ready(function() {
                var count = <?php echo $c - 1; ?>; // substract 1 from $c
                $(".add").click(function() {
                    count = count + 1;
                    //$(\'#price_items\').append(\'<li><label>Nr :</label><input type="text" name="price_data[\' + count + \'][n]" size="10" value=""/><label>Description :</label><input type="text" name="price_data[\' + count + \'][d]" size="50" value=""/><label>Price :</label><input type="text" name="price_data[\' + count + \'][p]" size="20" value=""/><span class="remove">Remove</span></li>\');
                   $(\'#price_items\').append(\'<? echo implode(\'\',explode("\\n",Print_price_fileds(\'count\'))); ?>\'.replace(/count/g, count));
                    return false;
                });
                $(".remove").live(\'click\', function() {
                    $(this).parent().remove();
                });
            });
        </script>
        <style>#price_items {list-style: none;}</style>
    <?php
    echo \'</div>\';
}


//Save product price
add_action(\'save_post\', \'save_detailss\');

function save_detailss($post_id){ 
global $post;


    // to prevent metadata or custom fields from disappearing... 
    if ( defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE ) 
    return $post_id; 
    // OK, we\'re authenticated: we need to find and save the data
    if (isset($_POST[\'price_data\'])){
        $data = $_POST[\'price_data\'];
        update_post_meta($post_id,\'price_data\',$data);
    }else{
        delete_post_meta($post_id,\'price_data\');
    }
} 
我不断有人问我如何将数据打印到fronend中,因此:

$data = get_post_meta($post->ID,"price_data",true);
echo \'<ul>\';
    if (count($data) > 0){
        foreach((array)$data as $p ){
            if (isset($p[\'p\']) || isset($p[\'d\'])|| isset($p[\'n\'])){
                echo \'<li>Number: \'.$p[\'n\'].\' Description: \'.$p[\'d\'].\' Price: \'.$p[\'p\'].\'</li>\';
            }
        }
    }
echo \'</ul>\';

结束

相关推荐

在另一个metabox中使用WPAlChemy metabox值

我正在构建一个非常复杂的自定义帖子类型结构,我需要一些关于我的元盒的帮助。我想做什么:谢谢have_fields_and_multi() 功能,用户在简单的文本输入字段中输入数据(使用“添加新”按钮)应使用以前文本输入的值来构建select 另一个元框中的下拉列表</为了简单起见,这里有一个模型(也附在这篇文章后面):http://idzr.org/0c95我有第一部分工作,很简单。但我不知道如何让第二部分发挥作用。如果我使用while($mb->have_fields_and_multi(\