get_option& update_option用于具有相同名称wordpress的多个输入字段

时间:2015-10-01 08:00:28

标签: php wordpress custom-fields

这是我的代码:

<ul>
<?php
        $vArgs = array(
            'posts_per_page' => -1,
            'offset' => 0,
            'order' => 'DESC',
            'orderby'  => 'date',
            'post_type' => 'video-list',
            'post_status' => 'publish',
            'suppress_filters' => 0
        );
        $video_list = get_posts($vArgs);
        if ($video_list) {
            $count = 1;
            foreach ($video_list as $video_item) {?>
            <li>
                <div class="admin_video_views_val bottom_view_col">
                    <input type="text" name="addon_post_views_count_meta[<?php echo $count; ?>]" id="addon_post_views_count_meta[<?php echo $count; ?>]" class="get_addon_post_views_count" value="<?php echo $post_views_countnew;?>">
                </div>
            </li>
            <?php
            $count++; 
            }
        } ?>
  </ul>

我创建了管理菜单,并使用以下代码显示输入字段值中的所有帖子视图:

<input type="text" name="addon_post_views_count_meta[<?php echo $count; ?>]" id="addon_post_views_count_meta[<?php echo $count; ?>]" class="get_addon_post_views_count" value="<?php $post_views_count = get_post_meta( $video_item->ID, 'post_views_count', true );if ( ! empty( $post_views_count ) ) {echo $post_views_count;}else{echo $post_views_countnew;}?>">

如何看待我输入文本字段,这意味着用户需要编辑计数。我写了这样的东西,但它没有正常工作,它不保存选项,但应该这样做,我该怎么做?

if(isset($_POST['addon_post_views_count_meta'])){
    for($i=0; $i<count($_POST['addon_post_views_count_meta']); $i++){
         if($_POST['addon_post_views_count_meta'][$i] >= 0){
             $post_views_countnew = esc_attr($_POST['addon_post_views_count_meta'][$i]);   
             update_option($_POST['addon_post_views_count_meta'][$i], $post_views_countnew);               
         }
    }
}

for($i=0; $i<count($_POST['addon_post_views_count_meta']); $i++){
    if($_POST['addon_post_views_count_meta'][$i] >= 0){
        $post_views_countnew = get_option($_POST['addon_post_views_count_meta'][$i]);
        echo $post_views_countnew;
    }
} 

0 个答案:

没有答案