2
임 꽤 php에 새로운 및 내 코드와 함께 몇 가지 문제가 있습니다. 필자는 며칠 동안이 문제에 고심하고 연구하고 있습니다. 행운을 빌어 편집하고 ... 형제를 도우며 문제를 해결하는 방법을 설명하십시오 !! 사용자가 게시물에 이미지 (들)을 추가 할 수 있도록 대시 보드 포스트 페이지에 사용자 정의 메타 필드를 추가하는 코드Wordpress 사용자 정의 메타 필드, 마지막 입력 필드는 "정의되지 않은 색인"을 가져 오지만 다시 게시 후 작동
와
는목적.
는
코드 작품을 발행하지만, 100 % 작동하지 않습니다. 다음과 같은 메타 필드에 이미지를 추가 하나의 이미지 만 추가
- 발생하는 모든 작동합니다.
: 하나 개 이상의 이미지를 추가
<td><input type="text" class="widefat" name="src[]" value="<?php if ($field['src'] != '') echo esc_attr($field['src']); ?>" /></td>
와 배열은 다음과 같습니다 :
라인 (123)은
Array
(
[0] => Array
(
[title] => image-name1.gif
[alt] => image description 1
[src] => http://my-host.com/art/wp-content/uploads/2014/04/test1.gif
)
[2] => Array
(
[title] => image-nam2.gif
[alt] => image description 2
[src] => http://my-host.com/art/wp-content/uploads/2014/04/test3.gif
)
[3] => Array
(
[title] => image-name3.gif
[alt] => image description 3
)
)
,536,
- 마지막 이미지를 다시 추가하고 모든 게시물을 업데이트 할 때 작품!
연구
나는 그것이 경우에만 입력 필드 excists를 작동하기 때문에 필드의 값을 가지고 있다면 어떤 방법으로,의 src 입력 필드를 검사하는 기능 뭔가 될 것 같아요.
메타 필드를 저장하기 전에 배열을 만드는 마지막 for 루프에 뭔가 잘못 될 수도 있습니다.
기능.PHP :
// add meta box to screen
function add_meta_boxes() {
add_meta_box('image-meta-fields',
'Add Image to Post',
'image_meta_field_display',
'post',
'normal',
'high');
}
add_action('admin_init', 'add_meta_boxes', 1);
// output the meta box content
function image_meta_field_display() {
global $post;
$image_meta_fields = get_post_meta($post->ID, 'image_meta_fields', true);
wp_nonce_field('image_meta_field_nonce', 'image_meta_field_nonce');
?>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('.add-row').on('click', function() {
var row = $('.empty-row.screen-reader-text').clone(true);
row.removeClass('empty-row screen-reader-text');
row.insertBefore('#image-meta-field-one tbody>tr:last');
return false;
});
$('.remove-row').on('click', function() {
$(this).closest('tr').next().remove();
$(this).closest('tr').remove();
return false;
});
//
$('.add-image').click(function() {
var send_attachment_bkp = wp.media.editor.send.attachment;
var button = $(this);
wp.media.editor.send.attachment = function(props, attachment) {
$(button).closest('tr').prev().children('td').eq(0).find('input').val(attachment.title); //set title
$(button).closest('tr').prev().children('td').eq(1).find('input').val(attachment.alt); // set alt (description)
$(button).closest('td').prev().children().val(attachment.url); // set url
wp.media.editor.send.attachment = send_attachment_bkp;
}
wp.media.editor.open();
return false;
});
});
</script>
<table id="image-meta-field-one" width="100%">
<thead>
<tr>
<th width="30%">Name</th>
<th width="50%">Description</th>
<th width="10%"></th>
</tr>
</thead>
<tbody>
<?php
if ($image_meta_fields) :
foreach ($image_meta_fields as $field) {
?>
<tr>
<td><input type="text" class="widefat" name="title[]" value="<?php if ($field['title'] != '') echo esc_attr($field['title']); ?>" /></td>
<td><input type="text" class="widefat" name="alt[]" value="<?php if ($field['alt'] != '') echo esc_attr($field['alt']); ?>" /></td>
<td><a class="button remove-row" href="#">DEL</a></td>
</tr>
<tr>
<th width="30%"></th>
<td><input type="text" class="widefat" name="src[]" value="<?php if ($field['src'] != '') echo esc_attr($field['src']); ?>" /></td>
<td><a class="button add-image" href="#" value="" >ADD IMAGE</a></td>
</tr>
<?php
}
else :
// show a blank one
?>
<tr>
<td><input type="text" class="widefat" name="title[]" /></td>
<td><input type="text" class="widefat" name="alt[]" /></td>
<td><a class="button remove-row" href="#">DEL</a></td>
</tr>
<tr>
<th width="30%"></th>
<td><input type="text" class="widefat" name="src[]" value=""/></td>
<td><a class="button add-image" href="#">ADD IMAGE</a></td>
</tr>
<?php endif; ?>
<!-- empty hidden one for jQuery -->
<tr class="empty-row screen-reader-text">
<td><input type="text" class="widefat" name="title[]" /></td>
<td><input type="text" class="widefat" name="alt[]" /></td>
<td><a class="button remove-row" href="#">DEL</a></td>
</tr>
<tr class="empty-row screen-reader-text">
<th width="30%"></th>
<td><input type="text" class="widefat" name="src[]" value="" /></td>
<td><a class="button add-image" href="#">ADD IMAGE</a></td>
</tr>
</tbody>
</table>
<p><a class="button add-row" href="#">Add More Images</a></p>
<?php
}
add_action('save_post', 'image_meta_field_save');
function image_meta_field_save($post_id) {
if (!isset($_POST['image_meta_field_nonce']) ||
! wp_verify_nonce($_POST['image_meta_field_nonce'], 'image_meta_field_nonce'))
return;
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return;
if (!current_user_can('edit_post', $post_id))
return;
$old = get_post_meta($post_id, 'image_meta_fields', true);
$new = array();
$titles = $_POST['title'];
$alts = $_POST['alt'];
$srcs = $_POST['src'];
$count = count($titles);
for ($i = 0; $i < $count; $i++) {
if ($titles[$i] != '') :
$new[$i]['title'] = wp_filter_post_kses($titles[$i]);
if ($alts[$i] != '')
$new[$i]['alt'] = wp_filter_post_kses ($alts[$i]);
if ($srcs[$i] != '')
$new[$i]['src'] = wp_filter_post_kses($srcs[$i]);
endif;
}
if (!empty($new) && $new != $old)
update_post_meta($post_id, 'image_meta_fields', $new);
elseif (empty($new) && $old)
delete_post_meta($post_id, 'image_meta_fields', $old);
}
이 같은 프런트 엔드에 메타 배열을 출력하기 :
내가 본 $titles
및 $alts
순서가오고 있다고이 변수를 검사
<?php
$get_images = get_post_meta(get_the_id(), 'image_meta_fields', true);
if (!empty($get_images)) {
foreach ($get_images as $key => $image) :
if ($key === 0)
echo "<img class='post-image' title='" . $image['title'] . "' alt='" . $image['alt'] . "' src='" . $image['src'] . "'/>";
else
echo "<img class='post-thn' title='" . $image['title'] . "' alt='" . $image['alt'] . "' src='" . get_image_thumbnail($image['src']) ."'/>";
endforeach;
}
?>