2014-07-22 6 views
0

제품 포스트 유형에 체크 박스를 추가하고 싶습니다. 그래서 코드를 작성했습니다게시물 메타 박스에 체크 박스를 추가하십시오.

add_action('add_meta_boxes', 'smashing_add_post_meta_boxes'); 
/* Create one or more meta boxes to be displayed on the post editor screen. */ 
function smashing_add_post_meta_boxes() { 

    add_meta_box(
    'smashing-post-class',  // Unique ID 
    esc_html__('Post Class', 'example'), // Title 
    'smashing_post_class_meta_box', // Callback function 
    'product',   // Admin page (or post type) 
    'side',   // Context 
    'default'   // Priority 
); 
} 

/* Display the post meta box. */ 
function smashing_post_class_meta_box($object, $box) { ?> 

    <?php wp_nonce_field(basename(__FILE__), 'smashing_post_class_nonce'); ?> 

    <p> 
    <label for="smashing-post-class"><?php _e("Add a custom CSS class, which will be applied to WordPress' post class.", 'example'); ?></label> 
    <br /> 
    <input class="widefat" type="checkbox" name="smashing-post-class" id="smashing-post-class" value="<?php echo esc_attr(get_post_meta($object->ID, 'smashing_post_class', true)); ?>" size="30" />What's New 
    </p> 
<?php } 


/* Save post meta on the 'save_post' hook. */ 
add_action('save_post', 'smashing_save_post_class_meta', 10, 2); 

/* Save the meta box's post metadata. */ 
function smashing_save_post_class_meta($post_id, $post) { 

    /* Verify the nonce before proceeding. */ 
    if (!isset($_POST['smashing_post_class_nonce']) || !wp_verify_nonce($_POST['smashing_post_class_nonce'], basename(__FILE__))) 
    return $post_id; 

    /* Get the post type object. */ 
    $post_type = get_post_type_object($post->post_type); 

    /* Check if the current user has permission to edit the post. */ 
    if (!current_user_can($post_type->cap->edit_post, $post_id)) 
    return $post_id; 

    /* Get the posted data and sanitize it for use as an HTML class. */ 
    $new_meta_value = (isset($_POST['smashing-post-class']) ? sanitize_html_class($_POST['smashing-post-class']) : ''); 

    /* Get the meta key. */ 
    $meta_key = 'smashing_post_class'; 

    /* Get the meta value of the custom field key. */ 
    $meta_value = get_post_meta($post_id, $meta_key, true); 

    /* If a new meta value was added and there was no previous value, add it. */ 
    if ($new_meta_value && '' == $meta_value) 
    add_post_meta($post_id, $meta_key, $new_meta_value, true); 

    /* If the new meta value does not match the old value, update it. */ 
    elseif ($new_meta_value && $new_meta_value != $meta_value) 
    update_post_meta($post_id, $meta_key, $new_meta_value); 

    /* If there is no new meta value but an old value exists, delete it. */ 
    elseif ('' == $new_meta_value && $meta_value) 
    delete_post_meta($post_id, $meta_key, $meta_value); 
} 

코드를 변경하여 체크 박스 값을 가져오고 다른 사람이 체크 박스를 선택했는지 확인하는 방법은 무엇입니까?

여기 입력 상자 "텍스트"만 "확인란"으로 변경했습니다. 하지만 하나의 확인란을 만들려면 어떻게 해야할지 모르겠다. 도와주세요. 나는 wordpress에서 새로운 사람이다.

답변

0

확인란의 값을 지정해야합니다. 이 값이 아직 설정되지 않은 경우 생각할 수도 있습니다.

value="<?php echo esc_attr(get_post_meta($object->ID, 'smashing_post_class', true)); ?>" 

새 게시물이 없습니다. 그래서 당신은

value="<?php if ($x=get_post_meta($object->ID, 'smashing_post_class', true)) {echo $x;}else{echo "whatever";} ?>" 

이미 post_meta에 값을 저장하는 등의 일을 할 수있다 ($ new_meta_value 참조) 그래서 코드가 존재하는 경우 값이 이미 데이터베이스에서 끌어 설정할 수 있습니다.

위의 작업을 다시 생각해 볼 수 있습니다. 체크 박스 값은 항상 지금부터 같을 것입니다. 그 값을 지금 원하는 값으로 설정할 수도 있습니다. 예 :

<input type="checkbox" name="whatever" value="true"> select me for fun 

당신은

당신의 저장 메타 박스 기능

$value= $_POST['whatever']; 

에 게시 된 값을 액세스하고 메타 (사용자 정의 값과 적합 wp_posts 해달라고 몇 가지 다른 값에 대한 별도의 데이터베이스 테이블)을 게시 할 수 저장할 수 있습니다

update_post_meta($post_id, '_keyname', $value);