• Resolved mross55

    (@mross55)


    Hi, I’m fairly new to WordPress.

    I’ve created a new custom field ‘brands’.
    At the bottom of a page there’s about 10 brands (logos).
    So far all looks good.

    I want to add a url link from the graphic image to its site. How do I do this? Can someone provide me with a code example please?

    This is in the current value field: https://mywebsite.com/wp-content/uploads/brandname.jpg

Viewing 6 replies - 1 through 6 (of 6 total)
  • When you’ve created a custom field, for example with the name link, you put the following type of code on the page where you want it to appear

    <?php $link = get_post_meta($post->ID, "link", true);
    if ($link !=='') {echo "$link";}  ?>

    This says the variable $link is going to be the content of the custom field that you’ve called link

    If there is some content in that custom field then print it out on the page.

    This will give you the url, but you now need to make it a link. So it would end up looking something like this:

    <a href="
    
    <?php $link = get_post_meta($post->ID, "link", true);
    if ($link !=='') {echo "$link";}  ?>
    
    "><img srce="somewhere.com/img.jpg" /></a>

    Thread Starter mross55

    (@mross55)

    Thanks for responding.

    I’m sorry, I don’t follow – novice!
    The developer created the custom fields, so that it’s easier to add them to the page.

    where do I put this code?:
    <?php $link = get_post_meta($post->ID, “link”, true);
    if ($link !==”) {echo “$link”;} ?>

    The developer will presumably have put this code already on the page.

    But it can go on any page where you want the link – if it’s at the bottom of a page, then it would be probably in a footer.php if the developer has such a thing, or maybe at the bottom of single.php or category.php – really, it could go anywhere. It all depends on how your theme has been designed.

    Thread Starter mross55

    (@mross55)

    Thanks again, I’ll have a play and see how it goes.
    Cheers

    Thanks richarduk, it works fine for me!

    how can i make it to open in a new window?

    this is how i use it = with image opening in a new window

    <a href=" 
    
    <?php 
    
    $link = get_post_meta($post->ID, "link", true); 
    
    if ($link !=='') {echo "$link";}  ?> 
    
    " target="_blank"><img srce="<?php $banner1 = get_post_meta($post->ID, 'banner1', true);
    
    echo wp_get_attachment_image($banner1); ?></a>
Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘How to add a url link in a value field?’ is closed to new replies.