WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] OK WP gurus! Possible to make custom field images linkable? (14 posts)

  1. kmort
    Member
    Posted 5 years ago #

    Here's the code I used to create my custom field.

    <span id="featured-image">
    <img src="<?php $values = get_post_custom_values("featuredpageimage");echo $values[0]; ?>"
    </span>

    Is there any way to make that image linkable?

    Thanks!

  2. stvwlf
    Member
    Posted 5 years ago #

    Is values a valid URL?

    if so wrap it in an A tag
    <?php $values = get_post_custom_values("featuredpageimage");?>
    ">Underline me

  3. kmort
    Member
    Posted 5 years ago #

    Do you mean in the value field of the WP dashboard where I've created my page?

    I.e. use this in the featuredpageimage value field?

    etc.

    I've tried that -- it doesn't seem to work.

  4. kmort
    Member
    Posted 5 years ago #

    Do you mean in the value field of the WP dashboard where I've created my page?

    I.e. use an <a href, img src code in the featuredpageimage value field?

    I've tried that -- it doesn't seem to work.

    (sorry for the double post, wanted to be clear which code I was using)

  5. kmort
    Member
    Posted 5 years ago #

    In fact, I can't even set my value to be a link. Is that a limitation of WP custom fields or have I mucked my code somewhere else, I wonder . . .

  6. stvwlf
    Member
    Posted 5 years ago #

    I'm sorry, i screwed up the code above by not putting it in code tags. It was supposed to say

    Is values a valid URL?
    if so wrap it in an A tag

    <?php $values = get_post_custom_values("featuredpageimage"); ?>
    <a href="<?php echo $values; ?>">Underline me</a>

    If that does not work then please post the full content of what the "value" of a typical featuredpageimage custom field is.

  7. kmort
    Member
    Posted 5 years ago #

    The value for featuredpageimage is the url pointing to the image.

    Maybe my question isn't clear.

    I can configure custom fields to point to images on my server -- no problem with that.

    What I'd like is for the image, when it displays on my page, to serve as an image link to third site. Put another way, I want the featuredpageimage value to behave as if it were

    <a href="URL_OF_OTHER_SITE_HERE"><img src="URL_OF_MY_IMAGE_HERE"></a>

    I've tried using that code as the custom field value. Doesn't seem to work.

  8. t31os
    Member
    Posted 5 years ago #

    So use another custom field... something like this maybe?

    Somewhere inside the loop, but before the rest of the HTML (just to keep it out the way).

    <?php $myimage = get_post_meta($post->ID, 'field_one', true);?>
    <?php $myimagelink = get_post_meta($post->ID, 'field_two', true);?>

    Where "field_one and field_two" is actually the custom field names..

    Then wherever you want to display the image.

    <?php
    $the_image = '';
    
    if($myimagelink) :
      $the_image .= '<a href="'.$myimagelink.'">';
    
      if($myimage) :
        $the_image .= '<img src="'.$myimage.'" border="0" alt="'.$myimagelink.'" />';
      endif;
    
      $the_image .= '</a>';
    echo $the_image;
    endif;
    ?>
  9. kmort
    Member
    Posted 5 years ago #

    PERFECT!!!

    Thanks so much!!!
    xxxooo

  10. t31os
    Member
    Posted 5 years ago #

    Oops that should actually be...

    <?php
    $the_image = '';
    if($myimagelink) : $the_image .= '<a href="'.$myimagelink.'">';
    
      if($myimage) : $the_image .= '<img src="'.$myimage.'" border="0" alt="'.$myimagelink.'" />';
      endif;
    
      $the_image .= '</a>';
    endif;
    echo $the_image;
    ?>

    The bottom 2 lines were the wrong way round.

    :)

  11. kmort
    Member
    Posted 5 years ago #

    Believe it or not it worked the first way too :-)

  12. oyohan
    Member
    Posted 5 years ago #

    Thank You!!! I've looked everywhere for this snippet of code!!! You Rock t31os!!!!!!!! That was a productive 5 hours.

  13. mydanioh
    Member
    Posted 4 years ago #

    Hi t31os,

    This is what I've been looking for. But I'd also like to track the links using onclick... is that possible?

  14. jlowry
    Member
    Posted 4 years ago #

    t31os, thanks so much! Is it possible to get a little more help with the code you provided please? I'm a total newbie at this. :-)
    Where do you declare the URL? Do you need 2 custom field names: one for the image and one for the URL? Sorry, just a bit confused. Thanks again!

Topic Closed

This topic has been closed to new replies.

About this Topic