Forums

[resolved] How to add a url link in a value field? (7 posts)

  1. mross55
    Member
    Posted 1 year ago #

    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: http://mywebsite.com/wp-content/uploads/brandname.jpg

  2. richarduk
    Member
    Posted 1 year ago #

    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>
  3. mross55
    Member
    Posted 1 year ago #

    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";} ?>

  4. richarduk
    Member
    Posted 1 year ago #

    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.

  5. mross55
    Member
    Posted 1 year ago #

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

  6. eleven.mary
    Member
    Posted 1 year ago #

    Thanks richarduk, it works fine for me!

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

  7. eleven.mary
    Member
    Posted 1 year ago #

    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>

Topic Closed

This topic has been closed to new replies.

About this Topic