如何使高级自定义字段成为 link?

How do I make an advanced custom field a link?

我在 WooCommerce 的产品页面上有一个自定义字段,代码如下

add_action('woocommerce_single_product_summary', 'subtitle_link', 15);

function subtitle_link() {
    global $post;
    $subtitle = get_post_meta($post->ID, 'link', true);
    if(!empty($subtitle)){

        echo __("\n<br />\n<br />Enter this competition for free:", '');
        echo '<h3>'.$subtitle.'</h3>';

    }
}

虽然这在前端显示正常,但 URL 不是 link。

HTML目标属性

  • _blank Opens the linked document in a new window or tab
  • _self Opens the linked document in the same frame as it was clicked (this is default)
  • _parent Opens the linked document in the parent frame
  • _top Opens the linked document in the full body of the window
  • framename Opens the linked document in a named frame
function subtitle_link() {
    global $post;

    // Make sure this works!
    $subtitle = get_post_meta($post->ID, 'link', true);

    // If the above works, remove this!
    $subtitle = 'http://www.google.com';

    if( $subtitle ) {
        echo __("\n<br />\n<br />Enter this competition for free:", '');
        echo "<h3><a href='" . $subtitle . "' target='_blank'><span class='complink'>" . $subtitle . "</span></a></h3>";
    }
}
add_action('woocommerce_single_product_summary', 'subtitle_link', 15);