在我的WordPress网站中,我为作者网站链接创建了一个自定义字段,但我不知道如何使其成为超链接。人们必须能够点击它来浏览该网站。
目前它只显示原始文本,例如:“www.example.com”
我的代码是:
<?php echo get_post_meta($post->ID, 'Author Website', true); ?>
答案 0 :(得分:0)
为避免HTML损坏,您需要首先检查链接是否存在,然后再显示它。
为此,您需要使用if语句:
if( $link = get_post_meta( $post->ID, 'Author Website', true ) )
对于链接本身,常规HTML锚标记如下所示:
<a href="http://www.example.com/">http://www.example.com/</a>
例如,我使用sprintf函数,它将用适当的值替换%s:
if( $link = get_post_meta( $post->ID, 'Author Website', true ) ) {
sprintf(
'<a href="%s" target="_blank">%s</a>',
esc_attr( $link ),
$link
);
}
在这个例子中,我使用esc_attr()来确保链接不会破坏页面布局。
希望这适合你:)