All Good Things Must Come To An End

As you all know, I have been doing WordPress plugins and supporting it for the past 6 years. These 6 years of my life, I have been through my polytechnic education, my national service as well as my university education.

I just graduated from university in December 2009 and have been looking for full-time jobs. I am offered a full-time job and will be starting work on 1st February 2010.

I regret to say that I am NOT ABLE to provide support for my plugins anymore due to my full-time job commitment. I will leave this forum open and let the community help one another.

However, I WILL still update my plugins whenever I can and you still can report bugs to me via email and I will try to fix it.


Author Topic: How to print image attachments?  (Read 623 times)

0 Members and 1 Guest are viewing this topic.

Offline phycel

  • Newbie
  • *
  • Posts: 4
    • View Profile
  • WordPress Version: WordPress 2.8.x
  • Theme Name: custom built themes
How to print image attachments?
« on: 05 January 2010, 10:35 »
   1. 2.9.1
   2. Custom Built
   3. Development URL - http://frontporch.clientpod.com/
   4. WP-Print 2.5
   5. PHP Version 5.2.6 | MySQL version: 5.0.16
   6. Possible Conflicting Plugins (N/A)

I'm working on a project where the images are not placed into the content area but instead are displayed by grabbing the post attachments.  When I put my code into the print-posts.php template, it is grabbing an image attachment of another post instead of the one I am trying to print.

Here is my print page - http://frontporch.clientpod.com/products/case-pieces/desks/french-satinwood-desk/print/
As you can see, it is pulling an image of a lamp instead of the desk image.

Within the loop of the print-posts.php template, I placed the following code:

Code: [Select]
<?php 
$args 
= array(
'order'          => 'ASC',
'post_type'      => 'attachment',
'post_parent'    => $post->ID,
'post_mime_type' => 'image',
'post_status'    => null,
'numberposts'    => 1,
);
$attachments get_posts($args);
if (
$attachments) {
foreach ($attachments as $attachment) {
echo wp_get_attachment_image($attachment->ID'large'false);
}
}
?>


Any help as to why its not grabbing the correct image would be greatly appreciated.  I also plan to use the WP-Email plugin for such a task.  Thanks again for your time and hard work on these great plugins.

Rene

Offline GaMerZ

  • lesterchan.net
  • Administrator
  • *****
  • Posts: 11,536
    • View Profile
  • WordPress Version: WordPress 2.9.x
  • Theme Name: lesterchan.net v3.4
Re: How to print image attachments?
« Reply #1 on: 05 January 2010, 14:52 »
I am guessing that the get_post() will corrupt the current loop aka query_posts() or the query_posts() replace the variable of get_post(). Try placing it outside the loop.

++ lesterchan.net - Lester Chan's Website

I regret to say that I am not able to provide support for my plugins anymore due to my full-time job commitment. I will leave this forum open and let the community help one another.

Offline phycel

  • Newbie
  • *
  • Posts: 4
    • View Profile
  • WordPress Version: WordPress 2.8.x
  • Theme Name: custom built themes
Re: How to print image attachments?
« Reply #2 on: 05 January 2010, 20:43 »
When I place it outside of the loop it acts the same, still grabbing the lamp image.  The snippet of code I'm using to grab the attachments is meant to be placed inside the loop and seems to work fine in my single.php template file, so what could be causing it to break in this instance?

I guess I could use a custom field for the print image, but its one additional step I was hoping to avoid.

Your thoughts are greatly appreciated.  :)

Offline GaMerZ

  • lesterchan.net
  • Administrator
  • *****
  • Posts: 11,536
    • View Profile
  • WordPress Version: WordPress 2.9.x
  • Theme Name: lesterchan.net v3.4
Re: How to print image attachments?
« Reply #3 on: 05 January 2010, 21:01 »
Did you try to echo the $post->ID to see it reflects correctly?

++ lesterchan.net - Lester Chan's Website

I regret to say that I am not able to provide support for my plugins anymore due to my full-time job commitment. I will leave this forum open and let the community help one another.

Offline phycel

  • Newbie
  • *
  • Posts: 4
    • View Profile
  • WordPress Version: WordPress 2.8.x
  • Theme Name: custom built themes
Re: How to print image attachments?
« Reply #4 on: 05 January 2010, 21:46 »
Ok, if I try echo $post->ID inside/outside the loop, nothing shows up.  If I try the_ID(); inside the loop, I get the correct ID.  For some reason $post->ID is not working the way it should.  I did test my code in a post and was able to get the correct ID to echo... its just not working in the print-posts.php template.  Of course, I know I must be doing something wrong because the template displays the correct title, content, and url.

Offline phycel

  • Newbie
  • *
  • Posts: 4
    • View Profile
  • WordPress Version: WordPress 2.8.x
  • Theme Name: custom built themes
Re: How to print image attachments?
« Reply #5 on: 06 January 2010, 02:54 »
Well, I figured there's gotta be a way around this minor issue... so I hacked my image attachment code a little bit.  Just thought I'd share.

Code: [Select]
<?php   
function recall($data)
{
   
$GLOBALS['d'] = $data;
   return 
'';
}
ob_start("recall");
the_id();
ob_end_clean();

$args = array(
'order'          => 'ASC',
'post_type'      => 'attachment',
'post_parent'    => $GLOBALS['d'],
'post_mime_type' => 'image',
'post_status'    => null,
'numberposts'    => 1,
);
$attachments get_posts($args);

if (
$attachments) {
foreach ($attachments as $attachment) {
echo wp_get_attachment_image($attachment->ID'large'false);
}
}
?>


Hopefully I can get it to work with the wp-email plugin too!  :)  Thanks for all your help with everything.  I always like using your plugins because I know how well you support and document everything.  As always, a pleasure.

Rene

Offline GaMerZ

  • lesterchan.net
  • Administrator
  • *****
  • Posts: 11,536
    • View Profile
  • WordPress Version: WordPress 2.9.x
  • Theme Name: lesterchan.net v3.4
Re: How to print image attachments?
« Reply #6 on: 06 January 2010, 06:18 »
Thanks, if you want to return instead of echo, use get_the_id()

++ lesterchan.net - Lester Chan's Website

I regret to say that I am not able to provide support for my plugins anymore due to my full-time job commitment. I will leave this forum open and let the community help one another.