Custom Field Value as Link URL? (2 posts)

  1. sgreve
    Posted 5 years ago #

    I'm kind of a PHP newb, so forgive me if this is a stupid question.

    Is it possible to use the value of a custom field as the URL in a link? For example:

    <a href="[custom field value]">Link Text</a>

  2. Mark / t31os

    Posted 5 years ago #

    Yes you can...fetch the data, then output inside the link HTML..


    <?php $my_meta = get_post_meta( $post->ID, 'my-field-name', true ); // see http://codex.wordpress.org/Function_Reference/get_post_meta ?>
    <?php if( $my_meta && '' != $my_meta ) : ?>
         <a href="<?php echo $my_meta ?>">My Link Text</a>
    <?php endif; ?>

    Code must be placed inside the loop(where $post->ID will be valid), or alternatively provided with a valid post ID(first parameter).

Topic Closed

This topic has been closed to new replies.

About this Topic


No tags yet.