GraphicRiver

Wordpress get_attached_imaged

1213 posts
  • Bought between 1 and 9 items
  • Exclusive Author
  • Has been a member for 2-3 years
  • Microlancer Beta Tester
webdesignerart says

Hi Guys i am in problem can’t echo my attached image in custom post type please check. the code in functions.php


$prefix = 'custom_';
    $custom_meta_fields = array(

        array(
            //'label'   => 'Textarea',
            'desc'  => 'A description for the field.',
            'id'    => $prefix.'textarea',
            'type'  => 'textarea'
        )
    );

    // Default metabox for custom post types.
    function avz_custom_meta_box() {

       // $post_types = get_post_types( array( 'public' => true ) );
       $post_types = get_post_types();
        foreach ( $post_types as $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');

        enter code here

    function show_avz_custom_meta_box() {
        global $avz_custom_meta_box_fields, $post;
        // Use nonce for verification
        echo '';

        // Begin the field table and loop
        echo '';
        foreach ($avz_custom_meta_box_fields as $field) {
            // get value of this field if it exists for this post
            $meta = get_post_meta($post->ID, $field['id'], true);
            // begin a table row with
            echo '
                    ';
                    switch($field['type']) {
                        case 'image':
                            $image = get_template_directory_uri().'/images/image.png';  
                            echo ''.$image.'';
                            if ($meta) { $image = wp_get_attachment_image_src($meta, 'medium'); $image = $image[0]; }               
                            echo    '

                                             Remove Image
                                            '.$field['desc'].'';
                        break;

                    } //end switch
            echo '';
        } // end foreach
        echo ''; // end table
    }

    // Save the 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 and save the data
        foreach ($avz_custom_meta_box_fields as $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 but in post page not showing. Guys help me. i know i am doing mistakes and bad coding. :(

1599 posts
  • Has been a member for 4-5 years
  • Sold between 1 000 and 5 000 dollars
  • Exclusive Author
  • Author had a Free File of the Month
  • Bought between 100 and 499 items
  • Referred between 10 and 49 users
  • United States
Graphic-Studio says

Is the Theme supporting Custom Fields?

1213 posts
  • Bought between 1 and 9 items
  • Exclusive Author
  • Has been a member for 2-3 years
  • Microlancer Beta Tester
webdesignerart says

Is the Theme supporting Custom Fields?

I just only add this code and in admin Custom Post Type showing the image upload form when i upload the image it shows in custom meta box area. But i can’t retrieve in post page. :(

1213 posts
  • Bought between 1 and 9 items
  • Exclusive Author
  • Has been a member for 2-3 years
  • Microlancer Beta Tester
webdesignerart says

Guys i need help.

3256 posts
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Exclusive Author
  • Interviewed on the Envato Notes blog
  • Beta Tester
  • Author had a File in an Envato Bundle
  • Author had a Free File of the Month
+4 more
ParkerAndKent says

Hi,

in single php you call:

get_post_meta($post->ID, $field['custom_image'], true))

Why $field[‘custom_image’] ??? What’s $field? single.php is for the front end… so you shouldn’t have the $field array set…

I guess you should just do:

get_post_meta($post->ID, 'custom_image', true))

Parker

1213 posts
  • Bought between 1 and 9 items
  • Exclusive Author
  • Has been a member for 2-3 years
  • Microlancer Beta Tester
webdesignerart says

Hi Guys i found some solution but i shows all images upload in post but i want to show only image from my meta box that i upload.

working code example but shows all image upload .


    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); $args = array( ‘post_type’ => ‘attachment’, ‘numberposts’ => -1, ‘post_status’ => null, ‘post_parent’ => $post->ID ); $attachments = get_posts( $args ); if ( $attachments ) { foreach ( $attachments as $attachment ) { echo ‘
  • ’; echo wp_get_attachment_image( $attachment->ID, ‘full’ ); echo ‘

    ’; echo apply_filters( ‘the_title’, $attachment->post_title ); echo ‘

  • ’; } } endwhile; endif; ?>
this code in not working but in this code you will get my custom post type meta box id:
if( $image_upload_id = get_post_meta($post->ID, $field['custom_image'], true)){
    $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'); 
}

So friend i want to show image which i upload through custom meta box.

3256 posts
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Exclusive Author
  • Interviewed on the Envato Notes blog
  • Beta Tester
  • Author had a File in an Envato Bundle
  • Author had a Free File of the Month
+4 more
ParkerAndKent says

Is ‘custom_image’ the key of the meta? You’re code isn’t clear… why are you saving attachments into the custom fields?

Parker

1213 posts
  • Bought between 1 and 9 items
  • Exclusive Author
  • Has been a member for 2-3 years
  • Microlancer Beta Tester
webdesignerart says

Is ‘custom_image’ the key of the meta? You’re code isn’t clear… why are you saving attachments into the custom fields? Parker
custom_image is ID.
// Default metabox for custom post types.
function avz_custom_meta_box() {

   // $post_types = get_post_types( array( 'public' => true ) );
   $post_types = get_post_types();
    foreach ( $post_types as $post_type ) {
        if ( $post_type == 'page' || $post_type =='post' )
            continue;
        add_meta_box(
        'custom_image', 
        'Header Image Upload Box', 
        'show_avz_custom_meta_box', 
        $post_type, 
        'normal', 
        'high' );
    }

}
add_action('add_meta_boxes', 'avz_custom_meta_box');
3256 posts
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Exclusive Author
  • Interviewed on the Envato Notes blog
  • Beta Tester
  • Author had a File in an Envato Bundle
  • Author had a Free File of the Month
+4 more
ParkerAndKent says

^ Ok, so you’re creating a metabox with id custom_image.

Now, the ‘custom_image’ id will never retrun anything in the front, is the id of your metabox…

For sure you have some fields in this metabox and you save the values in specific custom fields through a key…

To call in the front end the custom field where you store the image you should know the key you used to create and update the custom field:

update_post_meta($post_id, $field[‘id’], $new);

The key you use here $field[‘id’] for sure returns a string… well that’s the string you should use when calling get_post_meta in the front :)

What’s the $field[‘id’] for the image? You should know that…

1213 posts
  • Bought between 1 and 9 items
  • Exclusive Author
  • Has been a member for 2-3 years
  • Microlancer Beta Tester
webdesignerart says

Is ‘custom_image’ the key of the meta? You’re code isn’t clear… why are you saving attachments into the custom fields? Parker

Dude i done work like charm but i think its bad 2 time looops can you check, ALthough what i want this code does.


<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); 

if( $image_upload_id = get_post_meta($post->ID, $field['custom_image'], true)){
    $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'); 
}

 endwhile; endif; ?>

But in this page again i m using loop:


<?php
if( have_posts() ) {
  while( have_posts() ) {
    the_post();
    ?>

<?php the_content(); ?>

so what you think is fine, Two loops in page.

Because i can’t retrieve image without loop that’s why i use two loops.

by
by
by
by
by