高级自定义字段/ PHP问题

时间:2013-07-28 23:15:41

标签: wordpress wordpress-plugin advanced-custom-fields

如果在其他地方得到解答,我很抱歉,但我在这里遇到此ACF代码的问题:http://goo.gl/9onrFN我希望客户能够将投资组合网站链接(如果适用)添加到艺术家页面和链接会说"查看艺术家的网站"该链接将用户带到新窗口中的艺术家网站。除非在帖子的自定义字段中输入了网址,否则如何才能使此文字不可见?这是代码:

<p><?php the_field('contact_phone_number'); ?><br />
                    or <a href="mailto:<?php the_field('contact_email'); ?>"><?php the_field('contact_email'); ?></a><br />
                    View <a href="<?php the_field('artist_website'); ?>" target="_blank">Artist's Website</a></p>

提前致谢!

1 个答案:

答案 0 :(得分:2)

您可以检查ACF字段是否设置为:

if(get_field('artist_website')) {
    the_field('artist_website');
}

使用the_field将简单地回显字段的内容,而get_field将返回更有用的值。例如,您可以将上面的代码编写为:

注意:get_field simple返回字段的值,如果要检查是否输入了有效的URL,则必须使用正则表达式。

下面是你的代码,if语句执行空字段检查:

<p>
<?php the_field('contact_phone_number'); ?><br />
or <a href="mailto:<?php the_field('contact_email'); ?>"><?php the_field('contact_email'); ?></a>
<?php if(get_field('artist_website')) { ?>
    <br />View <a href="<?php the_field('artist_website'); ?>" target="_blank">Artist's Website</a>

    

您可以通过预先设置变量并在echo中包含HTML来更容易阅读代码:

<p>
<?php
$contact_phone_number = get_field('contact_phone_number');
$contact_email = get_field('contact_email');
$artist_website = get_field('artist_website');

echo "{$contact_phone_number}<br />";
echo "or <a href='mailto:{$contact_email}'>{$contact_email}</a><br/ >;
if($artist_website) {
     echo "View <a href='{$artist_website}' target='_blank'>Artist's website</a>";
}
?>
</p>
相关问题