【问题标题】:Advanced Custom Fields/PHP issue高级自定义字段/PHP 问题
【发布时间】:2013-07-28 15:32:28
【问题描述】:

如果这在其他地方得到了回答,我深表歉意,但我在这里遇到了这个 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>

提前致谢!

【问题讨论】:

    标签: wordpress advanced-custom-fields


    【解决方案1】:

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

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

    使用 the_field 将简单地回显字段的内容,而 get_field 将返回更有用的值。比如你可以把上面的代码写成:

    注意:get_field 简单返回字段的值,如果要检查是否输入了有效的 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>
    

    通过预先设置变量并在回显中包含 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>
    

    【讨论】:

    • 感谢您的回复。您能否详细说明第二个示例?我到底需要添加什么以及在哪里添加?
    • 想通了: 查看 艺术家的网站
    • @user2263554 很高兴它为您工作。您能否将我的答案打勾作为答案 - 您使用的代码与我给出的相同。
    猜你喜欢
    • 2016-01-20
    • 2019-03-23
    • 2013-07-14
    • 2015-01-21
    • 2020-05-02
    • 1970-01-01
    • 2017-12-16
    • 1970-01-01
    • 2012-01-17
    相关资源
    最近更新 更多