如何在echo函数中使用字符串变量


how to use string vars in echo function

我想在word press中的header.php模板中使用这段代码,以使用ACF插件字段。但它不起作用。例如CCD_ 1属性不起作用。

这是我的代码:

echo '<div align="center">';
//Rule Adress
if (the_field("rule") != 'http://' || the_field("rule") != '') {
    echo '<a href="' . the_field('rule') . '" role="button" class="Button Button--error" title="Rules" rel="nofollow" target="_blank"><i class="fa fa-fw fa-check"></i>Rules</a>';
}
//Cost
if (the_field("cost") != '0' || the_field("rule") != '') {
    echo '<a role="button" class="Button Button--info" style="cursor: default;"><i class="fa fa-fw fa-shopping-cart"></i>Cost: ' . the_field('cost') . ' Dolor</a>';
}
//Demo
if (the_field("demo") != 'http://' || the_field("demo") != '') {
    echo '<a href="' . the_field('demo') . '" role="button" class="Button Button--primary" title="Demo" rel="nofollow" target="_blank"><i class="fa fa-fw fa-heart"></i>Demo</a>';
} else {
    echo '<a role="button" class="Button Button--primary" title="Demo" style="cursor: default;"><i class="fa fa-fw fa-heart"></i>Demo</a>';
}
//Shots
if (the_field("shots") != 'http://' || the_field("shots") != '') {
    echo '<a href="' . the_field('shots') . '" role="button" class="Button Button--warning" title="Shots" rel="nofollow" target="_blank"><i class="fa fa-fw fa-desktop"></i>Shots</a>';
} else {
    echo '<a role="button" class="Button Button--warning" title="Shots" style="cursor: default;"><i class="fa fa-fw fa-desktop"></i>Shots</a>';
}
echo '</div">';

字段is-productrulecostdemoshot是我在wp-admin中定义的字段。

请帮我解决这个问题。

不要在if语句中使用the_field('field'),而是使用get_field('field')将其作为值而非对象返回。

例如:

<?php if (get_field("rule") != 'http://' || get_field("rule") != '') { ?>
    <a href="<?php the_field('rule'); ?>" role="button" class="Button Button--error" title="Rules" rel="nofollow" target="_blank"><i class="fa fa-fw fa-check"></i>Rules</a>
<?php } ?>

您也可以将此表达式缩短如下:

<?php 
    $rule = get_field("rule")
    if($rule || $rule != 'http://') { ?>
        <a href="<?php echo $rule; ?>" role="button" class="Button Button--error" title="Rules" rel="nofollow" target="_blank"><i class="fa fa-fw fa-check"></i>Rules</a>
<?php } ?>