Does anyone know how to query the database and display an image's custom field in the attachment page?
not resolvedDoes anyone know how to query the database and display an image's custom field in the attachment page?
David,
At this time, I don't think it's possible. It would take a change to the PhotoSmash core to allow for referencing an image by the Attachment ID...then you could use something like:
<?php echo do_shortcode("[psmash select_image='current_attachment_id' layout='my_layout']"); ?>
You could build all of your custom fields into the custom layout 'my_layout' and then display them that way. Unfortunately, the part that doesn't exist is the select_image='current_attachment_id'.
If you decide to build the code for that, let me know and I'll see about incorporating into the core.
Cheers,
Byron
David,
Actually, you could do it outside of PhotoSmash just using regular old PHP...here's the code:
<?php
$imageinfo = $wpdb->get_row("SELECT wp_bwbps_customdata.*, wp_bwbps_images.* FROM wp_bwbps_images LEFT OUTER JOIN wp_bwbps_customdata ON wp_bwbps_customdata.image_id = wp_bwbps_images.image_id WHERE wp_bwbps_images.wp_attach_id = " . (int)$post->ID);
if( $imageinfo ){
echo $imageinfo->my_custom_field;
}
?>
See if that works...
Cheer,s
Byron
That's great, Byron! Exactly what I was asking about, the regular old php way of doing it.
A note to anyone following along: my database tables have a prefix so they go like this: "wp_myprefix_bwbps_customdata" and also replace my_custom_field with the name of your custom field.
Thanks again.
Welcome!...and you're exactly right... you should replace wp_ with whatever you're using for your database prefix in your wp-config.php.
Cheers,
Byron
Hi,
Where exactly do I post this code:
<?php
$imageinfo = $wpdb->get_row("SELECT wp_bwbps_customdata.*, wp_bwbps_images.* FROM wp_bwbps_images LEFT OUTER JOIN wp_bwbps_customdata ON wp_bwbps_customdata.image_id = wp_bwbps_images.image_id WHERE wp_bwbps_images.wp_attach_id = " . (int)$post->ID);
if( $imageinfo ){
echo $imageinfo->my_custom_field;
}
?>
Thanks!
Mar
You could put it in your attachment.php file in your theme.
You basically have to put it where ever you want to show a given custom field.
Cheers,
Byron