wordpress - Custom post type image upload with meta box -
i can't find attached image in custom post type. code in functions.php
$prefix = 'custom_'; $custom_meta_fields = array( array( //'label' => 'textarea', 'desc' => 'a description field.', 'id' => $prefix.'textarea', 'type' => 'textarea' ) ); // default metabox custom post types. function avz_custom_meta_box() { // $post_types = get_post_types( array( 'public' => true ) ); $post_types = get_post_types(); foreach ( $post_types $post_type ) { if ( $post_type == 'page' || $post_type =='post' ) continue; add_meta_box( $prefix.'image', 'header image upload box', 'show_avz_custom_meta_box', $post_type, 'normal', 'high' ); } } add_action('add_meta_boxes', 'avz_custom_meta_box'); function show_avz_custom_meta_box() { global $avz_custom_meta_box_fields, $post; // use nonce verification echo '<input type="hidden" name="avz_custom_meta_box_fields_nonce" value="'.wp_create_nonce(basename(__file__)).'" />'; // begin field table , loop echo '<table class="form-table">'; foreach ($avz_custom_meta_box_fields $field) { // value of field if exists post $meta = get_post_meta($post->id, $field['id'], true); // begin table row echo '<tr> <td>'; switch($field['type']) { case 'image': $image = get_template_directory_uri().'/images/image.png'; echo '<span class="custom_default_image" style="display:none">'.$image.'</span>'; if ($meta) { $image = wp_get_attachment_image_src($meta, 'medium'); $image = $image[0]; } echo '<input name="'.$field['id'].'" type="hidden" class="custom_upload_image" value="'.$meta.'" /> <img src="'.$image.'" class="custom_preview_image" alt="" /><br /> <input class="custom_upload_image_button button" type="button" value="choose image" /> <small> <a href="#" class="custom_clear_image_button">remove image</a></small> <br clear="all" /><span class="description">'.$field['desc'].'</span>'; break; } //end switch echo '</td></tr>'; } // end foreach echo '</table>'; // end table } // save data function save_multibox_custom_meta($post_id) { global $avz_custom_meta_box_fields; // verify nonce if (!wp_verify_nonce($_post['avz_custom_meta_box_fields_nonce'], basename(__file__))) return $post_id; // check autosave if (defined('doing_autosave') && doing_autosave) return $post_id; // check permissions if ('page' == $_post['post_type']) { if (!current_user_can('edit_page', $post_id)) return $post_id; } elseif (!current_user_can('edit_post', $post_id)) { return $post_id; } // loop through fields , save data foreach ($avz_custom_meta_box_fields $field) { if($field['type'] == 'tax_select') continue; $old = get_post_meta($post_id, $field['id'], true); $new = $_post[$field['id']]; if ($new && $new != $old) { update_post_meta($post_id, $field['id'], $new); } elseif ('' == $new && $old) { delete_post_meta($post_id, $field['id'], $old); } } // enf foreach // save taxonomies $post = get_post($post_id); $category = $_post['category']; wp_set_object_terms( $post_id, $category, 'category' ); } add_action('save_post', 'save_multibox_custom_meta');
and in single.php
if( $image_upload_id = get_post_meta($post->id, $field['custom_image'], true)){ $img = $image_upload_id ['custom_image'][0]; echo wp_get_attachment_image($img, 'full'); }
but can't find attached image. in admin uploaded image show in post page not showing.
have tried access post attachments? see if there?
<?php $args = array( 'post_type' => 'attachment', 'numberposts' => null, 'post_status' => null, 'post_parent' => $post->id ); $attachments = get_posts($args); if ($attachments) { echo "<pre>"; print_r($attachments); echo "</pre>"; $attachment = $attachments[0]; // print full link attachment the_attachment_link($attachment->id, false); }else{ echo "no attachments post!"; } ?>
Comments
Post a Comment