早上好,我正在为客户建立一个网站,我正在使用Wordpress进行CMS,我的PHP知识非常有限。我正在使用自定义字段插件,我正在尝试在模板中添加if语句以显示元数据,如果字段在CMS中填写,这是我目前所拥有的,但它仍然显示即使没有填写元字段,也可以使用该按钮。
<?php if(get_field('owners_manual')) ?>
<?php { ?>
<a href="<?php the_field('owners_manual'); ?>" class="pdf-button" target=_"blank">Owners Manual</a>
<?php } ?>
<?php if(get_field('parts_booklet')) ?>
<?php { ?>
<a href="<?php the_field('parts_booklet'); ?>" class="parts-button" target=_"blank">Parts Booklet</a>
<?php } ?>
<?php if(get_field('sales_leaflet')) ?>
<?php { ?>
<a href="<?php the_field('sales_leaflet'); ?>" class="leaflet-button" target=_"blank">Sales Leaflet</a>
<?php } ?>
答案 0 :(得分:0)
试试这个
<?php if(get_field('owners_manual') != "") ?>
<?php { ?>
<a href="<?php the_field('owners_manual'); ?>" class="pdf-button" target=_"blank">Owners Manual</a>
<?php } ?>
<?php if(get_field('parts_booklet') != "") ?>
<?php { ?>
<a href="<?php the_field('parts_booklet'); ?>" class="parts-button" target=_"blank">Parts Booklet</a>
<?php } ?>
<?php if(get_field('sales_leaflet') != "") ?>
<?php { ?>
<a href="<?php the_field('sales_leaflet'); ?>" class="leaflet-button" target=_"blank">Sales Leaflet</a>
<?php } ?>
答案 1 :(得分:0)
解决
<!-- Owners Manual -->
<?php
if( get_field('owners_manual') ):
$owners_link = get_field('owners_manual');
?>
<a href="<?php echo $owners_link ?>" class="pdf-button" target=_"blank">Owners Manual</a>
<?php endif; ?>
<!-- Parts Booklet -->
<?php
if( get_field('parts_booklet') ):
$parts_link = get_field('parts_booklet');
?>
<a href="<?php echo $parts_link ?>" class="parts-button" target=_"blank">Parts Booklet</a>
<?php endif; ?>
<!-- Sales leaflet -->
<?php
if( get_field('sales_leaflet') ):
$sales_link = get_field('sales_leaflet');
?>
<a href="<?php echo $sales_link ?>" class="leaflet-button" target=_"blank">Sales Leaflet</a>
<?php endif; ?>