• Resolved sjbmaine

    (@sjbmaine)


    I have a custom field called website. I am displaying this in the sidebar, so it’s called from outside the loop. The code below displays the url (ex: www.mysite.com). What I cannot figure out how to do is to turn this into a hyperlink.

    <?php
    global $wp_query;
    $postid = $wp_query->post->ID;
    echo get_post_meta($postid, 'website', true);
    ?>

    Susan

Viewing 5 replies - 1 through 5 (of 5 total)
  • Try this

    <?php
    global $wp_query;
    $postid = $wp_query->post->ID;
    $link = get_post_meta($postid, 'website', true);
    echo "<a href=\"$link\">$link</a>";
    ?>

    Edit: Okay, I see your response. I’m guessing that you aren’t including the http:// bit in your “website” field. You either need to do that, or use this snippet instead:

    <?php
    global $wp_query;
    $postid = $wp_query->post->ID;
    $link = get_post_meta($postid, 'website', true);
    $protocol = is_ssl() ? 'https:' : 'http:';
    echo "<a href=\"$protocol//$link\">$link</a>";
    ?>
    Thread Starter sjbmaine

    (@sjbmaine)

    Thanks for the suggestion. I tried this and it takes me to the WordPress page cannot be found page rather than the url listed in my custom field “website”.

    My custom field contains data similar to www.mysite.com which should take the user off the WordPress site to the URL contained in the field.

    Okay, I’ve updated my answer above. Try that second snippet.

    Thread Starter sjbmaine

    (@sjbmaine)

    Beautiful! It works. Thank you so much.
    Susan

    Sweet! If you can mark this thread as resolved using the dropdown on the right side of this page it will let people know it is solved.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Custom field outside loop displayed as a hyperlink’ is closed to new replies.