Displaying a featured image (only img url) as the img src?

I have the following code:

if (themedy_get_option('product_gallery')) { add_action('genesis_post_content', 'themedy_product_images', 1); }
function themedy_product_images() { ?>
    <?php 
    global $post;
    $images = get_children( array( 'post_parent' => $post->ID, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'numberposts' => 999, 'order'=> 'ASC', 'orderby' => 'menu_order' ) ); 
    if ( $images ) { ?>
<div class="twocol-one">
<?php echo get_the_post_thumbnail( $post->ID, 'product-400x600' ); ?>
    <div class="product_images">
        <div class="container">
            <?php
            foreach ( $images as $image ){
                $gallery_image_info = wp_get_attachment_image_src( $image->ID, 'full' );
                $gallery_image = $gallery_image_info[0];

                $gallery_thumb_info = wp_get_attachment_image_src( $image->ID, 'Gallery Thumb' );
                $gallery_thumb = $gallery_thumb_info[0];

                $gallery_image_alt = get_post_meta($image->ID, '_wp_attachment_image_alt', true) ? trim(strip_tags( get_post_meta($image->ID, '_wp_attachment_image_alt', true) )) : trim(strip_tags( $image->post_title ));

                echo '<a href="'.$gallery_image.'" title="'.$gallery_image_alt.'" class="fancybox" rel="single-gallery"><img src="' . $gallery_thumb . '" /></a>';
            }
            ?>
        </div>
    </div>
</div>

Issue #1: Where it says:

<?php echo get_the_post_thumbnail( $post->ID, 'product-400x600' ); ?>

I need it to say

<a id="product" class="MagicZoomPlus" href="https://wordpress.stackexchange.com/questions/43615/<?php echo get_the_post_thumbnail( $post->ID,"large' ); ?> " rel="selectors-class: Active"><img src="https://wordpress.stackexchange.com/questions/43615/<?php echo get_the_post_thumbnail( $post->ID,"product-400x600' ); ?> " alt="" /></a>`

Where it echo’s the images (large and product-400×600), I need it to only echo the img url, without all the other html that comes with it. How can i fix my above code to have it rendered the way I require?

Issue #2: Where it says:

echo '<a href="'.$gallery_image.'" title="'.$gallery_image_alt.'" class="fancybox" rel="single-gallery"><img src="' . $gallery_thumb . '" /></a>';

It is a loop of all the other images in that gallery. For each image in the loop..
I need it to say

<a class="Selector" href="https://wordpress.stackexchange.com/questions/43615/<?php echo get_the_post_thumbnail( $post->ID,"large' ); ?>" rel="zoom-id:product" rev="https://wordpress.stackexchange.com/questions/43615/<?php echo get_the_post_thumbnail( $post->ID,"medium' ); ?>"><img src="' . $gallery_thumb . '" alt="" /></a>

Again, same issue as #1 where I need it to echo just the image url/src.

The way it should function is like in this example:
http://www.magictoolbox.com/magiczoomplus/

Thank you in advance!

Edit

My first attempt as per Brian’s suggestion below for the featured image, works!

<?php
    $image = wp_get_attachment_image_src(get_post_thumbnail_id(), 'full');
    $image2 = wp_get_attachment_image_src(get_post_thumbnail_id(), 'product-400x600');
?>
<?php echo '<a id="product" class="MagicZoomPlus" href="'.$image[0].'" rel="selectors-class: Active"><img src="'.$image2[0].'" alt="" /></a>'; ?>

Now the question is, how do I go about this for the image gallery items?

1 Answer
1

Here are the functions you need to be aware of:

  • http://codex.wordpress.org/Function_Reference/get_post_thumbnail_id
  • http://codex.wordpress.org/Function_Reference/wp_get_attachment_url
  • http://codex.wordpress.org/Function_Reference/wp_get_attachment_image_src
  • http://codex.wordpress.org/Function_Reference/wp_get_attachment_image

Example sans size:

echo wp_get_attachment_url(get_post_thumbnail_id());

Example with size:

$image = wp_get_attachment_image_src(get_post_thumbnail_id(), 'large');
echo $image[0]; //as HREF in your A tag

You can grab all of the gallery images as follows:

$args = array(
    'order'=> 'ASC',
    'post_mime_type' => 'image',
    'post_parent' => $post->ID,
    'post_type' => 'attachment'
);

$attachments = get_children( $args );

foreach($attachments as $attachment){
    //use $attachment->ID to retrieve attachment information
    $image = wp_get_attachment_image_src($attachment->ID, 'large');
}

Leave a Comment