-2

我想在wordpress的header.php模板中使用此代碼來使用ACF插件字段。 但它不起作用。如href屬性不起作用。如何在echo函數中使用字符串變量

這是我的代碼:

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是我的領域是在可溼性粉劑管理員定義的。

請幫我解決這個問題。

回答

0

而不是在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 } ?> 
+0

感謝@asherstoppard。我不能在'if'語句中使用the_field()嗎?這段代碼是否工作沒有任何錯誤? – 2015-03-02 16:00:24

+0

完全沒問題,修正後爲你做了什麼? – asherstoppard 2015-03-09 09:41:04

相關問題