1

I guess I need to know how to call meta data into metaboxes for beginners.

I'm using

$posts = get_posts(array(
        'post_type'   => 'slidertype',
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'fields' => 'ids'
        )
    );
    //loop over each post
    foreach($posts as $p){
        //get the meta you need form each post
        $thumb = get_post_meta($p,"_thumbnail_id",true);

        echo $thumb;

To try to get thumbnails of my custom post type 'slidertype' to show up in a page's metabox. Right now it's displaying a number which doesn't make sense to me. No advice is bad advice guys! Thanks.

2 Answers 2

0

You can get the image from your $thumb integer with the following additional code:

$thumb = get_post_meta($p,"_thumbnail_id",true);
$image = wp_get_attachment_image_src($thumb);
echo $image[0];

This will return the URL of the image, just put that in an img tag to display it.

1
  • Oh wow! I was so close but thanks so much. I'm trying to make a sortable list for my slider this way so I believe that I'll prob need the id's... Not sure. Thanks again. Sep 17, 2012 at 21:38
0

You can also use get_the_post_thumbnail without having to first fetch the ID:

foreach( $posts as $p ){
    echo get_the_post_thumbnail( $p->ID, 'thumbnail' );
}

Or using setup_postdata you can use template tags that only work "in the loop":

foreach( $posts as $p ){
    setup_postdata( $p );
    the_post_thumbnail( 'large' );
}
wp_reset_postdata();
1
  • This is awesome. I wish I could vote you but I can't till lvl 15 sry. Sep 17, 2012 at 21:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.