I have include PHP-Code inside a href-tag – and it doesn’t work!

<?php $image = get_post_meta($post->ID, 'image_medium', true);
    if ( $image != '' ) {?> 

            <a href="<?php wp_get_attachment_image($image, 'category-large-thumb' ); ?>"  rel="enlargeimage" rev="targetdiv:loadarea,trigger:mouseover"><?php echo wp_get_attachment_image($image, 'category-small-thumb'); ?></a>

            <?php } ?>

The source code shows an empty href (href=““)

I checked it with a “direct” pathway (href=”/img.jpg”), like below, and it works

<?php $image = get_post_meta($post->ID, 'image2_medium', true);
    if ( $image != '' ) {?> 
        <a class="fancybox" href="<?php bloginfo('template_directory');?>/1_b.jpg" rel="enlargeimage" rev="targetdiv:loadarea,trigger:mouseover"><img src="<?php bloginfo('template_directory');?>/1_s.jpg" /></a>
    <?php } ?>

Where is my fault? How can I integrate the (loop-) php-code into the href-tag? Thanks Petra


You're using a get function, which does not echo by default. Update the PHP portion to the following:

<?php echo wp_get_attachment_image($image, 'category-large-thumb' ); ?>

Also, wp_get_attachment_image() returns a full image object - so I don't think that's what your after.

It also most seems like you'd just want the_attachment_link:

<?php the_attachment_link( $id, $fullsize, $deprecated, $permalink ); ?>

So - it'd turn into:

<?php the_attachment_link(get_post_meta($post->ID, 'image_medium', true)); ?>
  • Thanks for the solutions – but it didn’t work inside a href-construct. I want to show small Images on one side, and by hovering shown larger image version shown beside that (and by click on the large image, fancybox open) (something like this danidraws.com ) With thumbnailviewer2 and Fancybox it works without php-code or with direct image pathway. my questions: - is where a chance to integrate php-code into href thats include custom post type variables and thumbnailviewer2 options - or is there an other way, to create these preview pictures – Petra Oct 2 '12 at 11:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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