【问题标题】:If Custom Field Value : display, if not display another Custom Field如果自定义字段值:显示,如果不显示另一个自定义字段
【发布时间】:2017-05-22 12:09:29
【问题描述】:

我正在尝试在我的 wordpress 网站上显示 2 个不同的值:skuar.com 应该是,点击任何缩略图(中心标志)后

如果('music')中有字段值:显示

否则,在 ('image') 字段中显示值

其实不行,代码好不好?

<?php
$music = get_field('music');
$image = get_field('image');

if ($music!=''){ ?>
<iframe width="600" height="166" scrolling="no" class="lightbox" class="frame" id="featherlight" frameborder="no" src="https://w.soundcloud.com/player/?url=<?php the_field('music'); ?>&amp;color=1b1e25&amp;theme_color=1b1e25&amp;auto_play=false&amp;hide_related=false&amp;show_comments=true&amp;show_user=true&amp;show_reposts=false"></iframe> 

<?php } else { ?>

<img src="<?php $image ?>" />
<?php endif; ?> 

<?php } ?>

<?php
    if ( has_post_thumbnail() ) {
    the_post_thumbnail('post-thumbnails');
    }
?>

谢谢

【问题讨论】:

    标签: php wordpress if-statement advanced-custom-fields custom-fields


    【解决方案1】:

    如果get_field() 为空,它实际上返回false,而不是空字符串。因此,您的代码始终在运行 iframe,因为 $music 绝不是空字符串(它是 false 或您设置的某个值)。试试这个:

    <?php if( get_field( 'music' ) ): ?>
      <iframe width="600" height="166" scrolling="no" class="lightbox" class="frame" id="featherlight" frameborder="no" src="https://w.soundcloud.com/player/?url=<?php the_field( 'music' ); ?>&amp;color=1b1e25&amp;theme_color=1b1e25&amp;auto_play=false&amp;hide_related=false&amp;show_comments=true&amp;show_user=true&amp;show_reposts=false"></iframe>
    <?php else: ?>
      <img src="<?php the_field( 'image' ); ?>" />
    <?php endif; ?>
    

    【讨论】:

    • 不,它不起作用。我尝试了不同的选项,但仍然不起作用,我不知道为什么。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-21
    • 2020-04-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多