2017-12-28 7 views
0

사용자 지정 메타 상자 선택 옵션을 저장하려고 벽에 머리를 두드렸습니다. 드롭 다운에 여러 옵션이 있기 때문에 실제로 값을 저장하는 대상을 알아내는 데 어려움을 겪고 있습니다. select name="XXX"을 타겟팅하려고하지만 운이 아직 없습니다. 어떤 도움이라도 대단히 감사하겠습니다. 어떤 도움을 크게 감상 할 수WordPress 사용자 지정 메타 상자에서 선택 옵션을 저장하는 방법

<?php 
if (isset($_POST['_accessories_product_one']) { 
    update_post_meta($post_id, '_accessories_product_one', $_POST['_accessories_product_one']); 
    } ?> 

:

<?php 
    $accessory_product_args = [ 
     'posts_per_page' => -1, 
      'tax_query' => [ 
       'relation' => 'AND', 
       [ 
        'taxonomy' => 'product_cat', 
        'field' => 'slug', 
        'terms' => 'accessories' 
       ], 
      ], 
     'post_type' => 'product', 
     'orderby' => 'title', 
    ]; 
    $accessory_product = new WP_Query($accessory_product_args); ?> 

    <select name="_accessories_product_one" id="_accessories_product_one" class="widefat"> 
     <?php while ($accessory_product->have_posts()) : $accessory_product->the_post(); 
      $title_one = get_the_title(); 
      $postid_one = get_the_ID(); ?> 
      <option value="<?=$postid_one?>" <?php selected('_accessories_product_one[select_field_0]', $postid_one); ?>> 
       <?=$title_one?> 
      </option> 
     <?php endwhile; ?> 
    </select> 

    <?php 
//this below is used for testing to see if i have saved the value or not: 
    $dropdown_option = get_option('_accessories_product_one'); // Array 
    $dropdown_value = $dropdown_option ['select_field_0']; // Option value 

    var_dump($dropdown_option); 
    var_dump($dropdown_value); 
    ?> 

이 코드는 절약은 다음과 같습니다

다음은 옵션을 선택 드롭 다운을 보여 내 코드입니다.

편집 : 나는 woocommerce 제품 화면에서 이것을 사용하고 있습니다 - 페이지 편집, 포스트 편집 또는 플러그인 편집 화면이 아닙니다.

function custom_product_basic_load() { 

    add_action('add_meta_boxes_product' , 'custom_product_basic_add_meta_boxes_product'); 
    add_meta_box('custom_product_basic_metabox' , __('Product Layout') , 'custom_product_basic_metabox' , 'product' , 'normal' , 'high'); 
    add_action('save_post' , 'custom_product_basic_save_post' , 10 , 3); 

} 
add_action('load-post.php' , 'custom_product_basic_load'); 
add_action('load-post-new.php' , 'custom_product_basic_load'); 

function custom_product_basic_metabox($post) {?> 

     <input type="hidden" name="product_type" value="simple" /> 

     <?php 
     $accessory_product_args = [ 
      'posts_per_page' => -1, 
       'tax_query' => [ 
        'relation' => 'AND', 
        [ 
         'taxonomy' => 'product_cat', 
         'field' => 'slug', 
         'terms' => 'accessories' 
        ], 
       ], 
      'post_type' => 'product', 
      'orderby' => 'title', 
     ]; 
     $accessory_product = new WP_Query($accessory_product_args); ?> 

     <select name="_accessories_product_one" id="_accessories_product_one" class="widefat"> 
      <?php while ($accessory_product->have_posts()) : $accessory_product->the_post(); 
       $title_one = get_the_title(); 
       $postid_one = get_the_ID(); ?> 
       <option value="<?=$postid_one?>" <?php selected('_accessories_product_one[select_field_0]', $postid_one); ?>> 
        <?=$title_one?> 
       </option> 
      <?php endwhile; ?> 
     </select> 

     <?php 
     $dropdown_option = get_option('_accessories_product_one'); // Array 
     $dropdown_value = $dropdown_option ['select_field_0']; // Option value 

     var_dump($dropdown_option); 
     var_dump($dropdown_value); 
     ?> 
    <?php } 

    function custom_product_basic_save_post($post_id) { 
    if (isset($_POST['_accessories_product_one'])) { 

      update_post_meta($post_id, '_accessories_product_one', $_POST['_accessories_product_one']); 
     } 
    } 
+0

코드를 후크로 게시하십시오. – vel

+0

@vel @ – HollerTrain

답변

0

이 시도 : 여기

내가 사용하고있는 전체 코드의 더 자세한 붙여 넣기입니다. post_meta에 값을 저장합니다. 하지만 에서 오는 get_option. 게시물 메타 값을 가져 오기 위해 get_option을 사용하지 마십시오.

function custom_product_basic_load() { 

    add_action('add_meta_boxes_product' , 'custom_product_basic_add_meta_boxes_product'); 
    add_meta_box('custom_product_basic_metabox' , __('Product Layout') , 'custom_product_basic_metabox' , 'product' , 'normal' , 'high'); 
    add_action('save_post' , 'custom_product_basic_save_post' , 10 , 3); 

} 
add_action('load-post.php' , 'custom_product_basic_load'); 
add_action('load-post-new.php' , 'custom_product_basic_load'); 

function custom_product_basic_metabox($post) {?> 

    <input type="hidden" name="product_type" value="simple" /> 

    <?php 

    echo $productLayout = get_post_meta($post->ID, '_accessories_product_one',true); 

    $accessory_product_args = [ 
     'posts_per_page' => -1, 
      'tax_query' => [ 
       'relation' => 'AND', 
       [ 
        'taxonomy' => 'product_cat', 
        'field' => 'slug', 
        'terms' => 'accessories' 
       ], 
      ], 
     'post_type' => 'product', 
     'orderby' => 'title', 
    ]; 
    $accessory_product = new WP_Query($accessory_product_args); ?> 

    <select name="_accessories_product_one" id="_accessories_product_one" class="widefat"> 
     <?php while ($accessory_product->have_posts()) : $accessory_product->the_post(); 
      $title_one = get_the_title(); 
      $postid_one = get_the_ID(); ?> 
      <option value="<?=$postid_one?>" <?php selected($productLayout, $postid_one); ?>> 
       <?=$title_one?> 
      </option> 
     <?php endwhile; ?> 
    </select> 
    <?php 
} 

function custom_product_basic_save_post($post_id) { 
    if (isset($_POST['_accessories_product_one'])) { 

     update_post_meta($post_id, '_accessories_product_one', $_POST['_accessories_product_one']); 
    } 
} 
+0

많은, 많은 감사를 업데이트했습니다! – HollerTrain