WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question

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)); ?>
share|improve this answer
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 posting your answer, you agree to the privacy policy and terms of service.

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