[英]WordPress - IF statement not working within archive.php
我正在寻找一个简单的IF语句,以检查是否选择了自定义字段,如果是,则添加代码“ display:none”。
我有要添加的代码:
if( get_field('my_custom_field') )
{
echo "";
}
else
{
echo "style='display:none'";
}
但是,如果我在需要的地方添加此代码,则会抛出错误,而且由于放置在任何地方,无论我添加了什么其他php类型代码,都会抛出错误,我相信这是因为它是某种形式of loop不允许这样做,希望您能够提供帮助,这是它位于其中的代码:
$offices.= '
<div class="office_section" id="office_part_'.$locator.'">
<h1>'.$officetitle.'</h1>
'.$room_specs.'
<div class="office_dimensions_holder" *** this is where I would like to
add custom IF statement, but doesn't allow me to ***>
<div class="dimensions">
<div class="dimension_sml">Dimensions: </div>
'.$dimension1.'
我想这是因为它在$ offices。='调用中,因此请欣赏正确的语法以允许我检查最近添加的自定义字段是否为true,如果不是,则添加display:none。
看起来像这个关于php语法的问题...正确的应该看起来像:
$offices.= '
<div class="office_section" id="office_part_'.$locator.'">
<h1>'.$officetitle.'</h1>
'.$room_specs.'
<div class="office_dimensions_holder" '.(get_field('my_custom_field') ? "" : "style='display:none'").'>
<div class="dimensions">
<div class="dimension_sml">Dimensions: </div>
'.$dimension1.'
我最终在代码外输入了此代码,然后在代码内调用了条件代码:
$showdimensions = get_field('show_dimensions');
if(!empty($showdimensions)){
$showdimensions = 'style="display:none"';
} else {
$showdimensions = '';
}
然后在原始代码内添加:
'.$showdimensions.'
拉入显示:如果未选中该复选框,则无。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.