1

I created a custom post type for Products. I can display all the fields no problem (either in a list or a single-product.php). I wanted to use the Media library and associate an uploaded picture with the Product post type (mostly to handle all the thumbnail work automatically). I uploaded a photo to the media library and associated with a specific Product post type.

 $args = array(
        'label' => __('Products'),
        'labels' => $labels,
        'public' => true,
        'can_export' => true,
        'show_ui' => true,
        '_builtin' => false,
        'capability_type' => 'post',
        'menu_icon' => get_bloginfo('template_url').'/functions/images/product.png',
        'hierarchical' => false,
        'rewrite' => array( "slug" => "product" ),
        'supports' => array('title', 'thumbnail'), //MAYBE add thumbnail later!
        'show_in_nav_menus' => true

        );

I've tried everything to display the thumbnail and picture for the Product but it's not working: the_post_thumbnail();, get_the_post_thumbnail but nothing works. Here's my single-product.php:

      <div class="post_content">
        <?php 
            $attachment_args = array(
                'post_type' => 'attachment',
                'numberposts' => -1,
                'post_status' => null,
                'post_parent' => $post->ID
                    );

            $attachment = get_posts($attachment_args); ?>
        <img src="<?php echo wp_get_attachment_image_src($attachment->ID, 'full', false); ?>"  />


        <p><?php echo get_post_meta($post->ID, $prefix.'description', true); ?></p>
        <ul>
        <?php //this gets the custom fields for the document links (PDF Files)
          $custom_field_keys = get_post_custom_keys();
          foreach ( $custom_field_keys as $key => $value ) {
            $valuet = trim($value);
              if ( '_' == $valuet{0} || $prefix != substr($valuet, 0, 4) || $prefix.'description' == $valuet )
              continue;
            $n = substr($valuet, -1);

            if ( $valuet == $prefix.'docname'.$n)
                echo '<li><a href="' . get_post_meta($post->ID, $prefix.'docurl'.$n, true) . '" target="_blank">' . get_post_meta($post->ID, $prefix.'docname'.$n, true) . '</a></li>';     
          }
        ?>
        </ul>
    </div><!-- end post_content -->

Am I missing something? Thank you!

1 Answer 1

3

the_post_thumbnail() echoes the full image tag including src, you're using it within your own image tag. If you want just the thumbnail src to use within your own tag, use wp_get_attachment_image_src instead.

also- unrelated, use the_title_attribute for alt and title attributes within tags instead of the_title.

EDIT- using wp_get_attachment_image_src:

<?php
$image_attributes = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full' );
?>
<img src="<?php echo $image_attributes[0] ?>" width="<?php echo $image_attributes[1]; ?>" height="<?php echo $image_attributes[2]; ?>" />
4
  • Ahh that makes more sense. How do I get the attachment id for that particular post? (first time working with media library) Reading up on the codex now though. THANKS! Commented Sep 18, 2012 at 2:53
  • Edited post above with new code. please see. i think i'm getting close! Thank you! Commented Sep 18, 2012 at 3:15
  • see my edited answer above
    – Milo
    Commented Sep 18, 2012 at 3:32
  • That did it! It displayed the image. I guess I'm confused how wordpress knows a thumbnail from an image (or does it do it all for you). Thank you! Commented Sep 18, 2012 at 3:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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