Custom Content Type Manager
[resolved] Converting related attachment ID to image source (6 posts)

  1. KS Web Designer
    Posted 1 year ago #

    Hey, so I have been wrapping my brain around this for a few hours and I must be missing something really obvious, so I was wondering if someone could help out.

    I have created a 'Banner' content type which has various custom fields to specify text and image for each banner. Using anoter custom field on a page I can relate multiple instances of banners to my page, and want to pull through the custom fields from those banners.

    Reading through the documentation I have got as far as pulling all the information through and spitting it out as structured HTML - EXCEPT for the image. All I can seem to get back is the attachment ID. I have dont similar things in the past using the wp_get_attachment_image_src() call - but this doesn't seem to work within the foreach statement.

    My code:

    $banners = get_custom_field('relatedBanners:to_array', 'get_post');
    	foreach ($banners as $b) {
    		//print_r($b); // this will show you EVERYTHING that's available in that post
    		$bannerTitle = $b['post_title'];
    		$bannerDescription = $b['bannerDescription'];
    		$buttonText = $b['buttonText'];
    		$bannerLink = $b['bannerLink'];
    		$bannerImg = $b['bannerImage'];
    		$banner_html[] = '<div class="indvdlBanner"><div class="overlay"><div class="title">' . $bannerTitle . '</div><div class="description">' . $bannerDescription . '</div><a href="' . $bannerLink . '" class="button" title="' . $bannerTitle . '">' . $buttonText . '</a></div></div><img src="' . $bannerImg . '" />';

    <?php echo implode("\n", $banner_html); ?>

    Can anyone help me convert the bannerImg variable from just the image ID to the source URL of the image?

    Thank you


  2. fireproofsocks
    Plugin Author

    Posted 1 year ago #

    See https://code.google.com/p/wordpress-custom-content-type-manager/wiki/get_post_OutputFilter (specifically example #5). You can use wp_get_attachment_image() or any of the related WP functions -- from your code it doesn't look like you are doing anything to convert the image ID.

  3. KS Web Designer
    Posted 1 year ago #

    Thanks for the reply.

    That's the documentation I used to get this far. I guess my question is a more fundamental WordPress one than specific to this mod but I'm not quite sure how to go about converting the id number to the src of the image.

    I guess I'd need to do something with the $bannerImg variable?

  4. fireproofsocks
    Plugin Author

    Posted 1 year ago #

    Yes, that's exactly what is outlined in that example. If $bannerImg is the image ID, then you can do any of the following:

    $img = wp_get_attachment_image($bannerImg);
    print $img; // full <img> tag
    // Or
    list($src, $h, $w) = wp_get_attachment_image_src($bannerImg, null, true);
    print '<img src="'.$src.'" height="'.$h.'" width="'.$w.'" />';
    // Or
    $img_src = CCTM::filter($bannerImg, 'to_image_src');
    print '<img src="'.$img_src.'" />';

    or any other related WP function.

    This is hands down the #1 question I get: how to convert an image ID (or post ID) to what it represents. I've tried to answer it as many places as possible (See https://code.google.com/p/wordpress-custom-content-type-manager/wiki/FAQ#My_Image/Relation/Media_Field_Only_Shows_Numbers._WTF?!? or https://code.google.com/p/wordpress-custom-content-type-manager/wiki/Image ), but as you mention, it's really fundamentally a WordPress question.

  5. KS Web Designer
    Posted 1 year ago #

    It would help if I learned to read eh!

    Thanks for taking the time to explain it (again). I guess the only other place to put this info is on the admin editor screen for the relation and image field types as it's the only place you can assume anyone using an image via your plugin will definitely be looking at.

    Thanks again for the help, and for the awesome plugin!

  6. fireproofsocks
    Plugin Author

    Posted 1 year ago #

    put this info is on the admin editor screen for the relation and image field types

    I believe that's been done too: http://www.screencast.com/t/TSSsqQJt8c

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • Custom Content Type Manager
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic