WordPress.org

Support

Support » How-To and Troubleshooting » Custom Field Value as Link URL?

Custom Field Value as Link URL?

  • 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>

Viewing 1 replies (of 1 total)
  • Yes you can…fetch the data, then output inside the link HTML..

    Example:

    <?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).

Viewing 1 replies (of 1 total)
  • The topic ‘Custom Field Value as Link URL?’ is closed to new replies.